EF 数据初始化
数据库不存在时重新创建数据库:
Database.SetInitializer<testContext>(new DropCreateDatabaseAlways<testContext>());
每次启动应用程序时创建数据库:
Database.SetInitializer<testContext>(new CreateDatabaseIfNotExists<testContext>());
模型更改时重新创建数据库:
Database.SetInitializer<testContext>(new DropCreateDatabaseIfModelChanges<testContext>());
从不创建数据库:
Database.SetInitializer<testContext>(null);
在EF自动生成的Context文件中添加如下:
using System.Data.Entity; using System.Data.Entity.Infrastructure; using Web.Models.Mapping; namespace Web.Models { public class testContext : DbContext { static testContext() { Database.SetInitializer<testContext>(null); } public testContext() : base("Name=testContext") { } public DbSet<Person> People { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Configurations.Add(new PersonMap()); } } }