Seed Database in Code-First:

You can insert data into your database tables during the database initialization process. This will be important if you want to provide some test data for your application or to provide some default master data for your application.

To seed data into your database, you have to create custom DB initializer, as you created in DB Initialization Strategy, and override the Seed method. The following example shows how you can provide default data for the Standard table while initializing the School database:

public class SchoolDBInitializer : DropCreateDatabaseAlways<SchoolDBContext>
{
protected override void Seed(SchoolDBContext context)
{
IList<Standard> defaultStandards = new List<Standard>(); defaultStandards.Add(new Standard() { StandardName = "Standard 1", Description = "First Standard" });
defaultStandards.Add(new Standard() { StandardName = "Standard 2", Description = "Second Standard" });
defaultStandards.Add(new Standard() { StandardName = "Standard 3", Description = "Third Standard" }); foreach (Standard std in defaultStandards)
context.Standards.Add(std); base.Seed(context);
}
}

Now, set this DB initializer class in context class as below.

public class SchoolContext: DbContext
{ public SchoolContext(): base("SchoolDBConnectionString")
{
Database.SetInitializer(new SchoolDBInitializer()); }
public DbSet<Student> Students { get; set; }
public DbSet<Standard> Standards { get; set; }
}

Entity Framework Code-First(19):Seed Data的更多相关文章

  1. Entity Framework Tutorial Basics(19):Change Tracking

    Change Tracking in Entity Framework: Here, you will learn how entity framework tracks changes on ent ...

  2. Entity Framework Tutorial Basics(33):Spatial Data type support in Entity Framework 5.0

    Spatial Data type support in Entity Framework 5.0 MS SQL Server 2008 introduced two spatial data typ ...

  3. Entity Framework Tutorial Basics(35):Local Data

    Local Data The Local property of DBSet provides simple access to the entities that are currently bei ...

  4. Entity Framework Code first(转载)

    一.Entity Framework Code first(代码优先)使用过程 1.1Entity Framework 代码优先简介 不得不提Entity Framework Code First这个 ...

  5. Entity Framework Code First (三)Data Annotations

    Entity Framework Code First 利用一种被称为约定(Conventions)优于配置(Configuration)的编程模式允许你使用自己的 domain classes 来表 ...

  6. Entity Framework Code First (二)Custom Conventions

    ---------------------------------------------------------------------------------------------------- ...

  7. Entity Framework Code First (一)Conventions

    Entity Framework 简言之就是一个ORM(Object-Relational Mapper)框架. Code First 使得你能够通过C#的类来描述一个模型,模型如何被发现/检测就是通 ...

  8. Entity Framework Tutorial Basics(11):Code First

    Code First development with Entity Framework: Entity Framework supports three different development ...

  9. Entity Framework Code First (七)空间数据类型 Spatial Data Types

    声明:本文针对 EF5+, Visual Studio 2012+ 空间数据类型(Spatial Data Types)是在 EF5 中引入的,空间数据类型表现有两种: Geography (地理学上 ...

随机推荐

  1. vue-cli 脚手架项目-package.json

    使用vue-cli脚手架新建的项目中,含有package.json. package.json是npm的配置文件,里面设定了脚本以及项目依赖的库. npm run dev 这样的命令就写在packag ...

  2. THUWC2017

    100+20+20=140 还是很菜... T1 在美妙的数学王国中畅游 一棵树每个点有一个函数(sin,exp,一次函数),支持加边,删边,单点修改,查询一条路径在 $x$ 处的点值和 sol: 题 ...

  3. Linux命令学习(18):route命令

    版权声明更新:2017-05-20博主:LuckyAlan联系:liuwenvip163@163.com声明:吃水不忘挖井人,转载请注明出处! 1 文章介绍 本文介绍了Linux下面的route命令. ...

  4. Unity Shader 创建程序纹理贴图

    创建一个脚本 附加到一个游戏体上 using UnityEngine;using System.Collections; public class ProceduralTexture : MonoBe ...

  5. 洛谷【P2024】[NOI2001]食物链

    浅谈并查集:https://www.cnblogs.com/AKMer/p/10360090.html 题目传送门:https://www.luogu.org/problemnew/show/P202 ...

  6. k近邻算法C++二维情况下的实现

    k近邻算法C++二维实现 这是一个k近邻算法的二维实现(即K=2的情况). #include <cstdio> #include <cstring> #include < ...

  7. jquery-post 异常

    报错:Client closed connection before receiving entire response 前端请求: $.ajax({ type : 'POST' , url : '/ ...

  8. 通过Python查看Azure VM的状态

    Azure的管理平台采用Restful API的方式实现管理.比如获取VM的管理API的各种操作的文档请参考: https://docs.microsoft.com/en-us/rest/api/co ...

  9. laravel config文件的使用

    好多东西 由于许多地方都要使用与将来可能发生更改 我们需要把它提取出来 作为配置文件来使用 这样将来要修改的时候 只需要修改一处即可 学习源头: https://blog.csdn.net/linyu ...

  10. LdapContext获取对象的属性

    //            dn = "cn=1,cn=Users,DC=域名,DC=COM";//            Attributes answer = ctx.getA ...