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. Office 2007在安装过程中出错

    1, 可能是因为c:\program files\common files\microsoft Shared\web server Extensions\40\bin目录下缺少Fp4autl.dll, ...

  2. CCControlSwitch 、CCControlSlider、CCControlButton

    /* *bool hasMoved(); 这里获取的不是开关是否正在被用户拨动,而是开关最终的状态是由用户手动拨动开关进行的, *还是用户点击开关进行的状态更改 */ CCControlSwitch* ...

  3. mysql 权限的相应修改

    mysql 权限的相应修改 UPDATE user SET Password = PASSWORD('bbbb') WHERE user = 'root';flush privileges; upda ...

  4. 编码不规范之将request传参到sevice中

    最近审核团队成员代码,发现确实有很多不规范的地方,其中问题之一就是将request.response.session等参数到传入到sevice层处理,在sevice中通过request对象获取到参数后 ...

  5. poj 1201 Intervals——差分约束裸题

    题目:http://poj.org/problem?id=1201 差分约束裸套路:前缀和 本题可以不把源点向每个点连一条0的边,可以直接把0点作为源点.这样会快许多! 可能是因为 i-1 向 i 都 ...

  6. CentOS下安装Python3.4

    系统环境:CentOS 7.2 CentOS7安装Python3.4 ,让Python2和3共存 编译需要的一些包: yum -y groupinstall "Development too ...

  7. 第一篇 PHP开发环境搭建以及多站点配置(基于windows 7系统)

    从今天开始,我将用PHP开发一些小的网站,大家知道LAMP(Linux)组合的优势,使PHP受到广大中小企业的喜欢.使PHP与JAVA,ASP三分天下,PHP具有跨平台性,所以在windows一样是可 ...

  8. maven工程编译成jar包

    在pom文件的project节点下增加build节点,mvn package即可 <build> <plugins> <plugin> <artifactId ...

  9. (转)详解C#中的反射

    本文转载自:http://blog.csdn.net/educast/article/details/2894892 两个现实中的例子:1.B超:大家体检的时候大概都做过B超吧,B超可以透过肚皮探测到 ...

  10. linux下mysql配置文件my.cnf最详细解释

    MySQL配置文件在Windows下叫my.ini,在MySQL的安装根目录下:在Linux下叫my.cnf,该文件位于/etc/my.cnf. 可以查找下:find / -name my.cnf m ...