策略一:数据库不存在时重新创建数据库
1 Database.SetInitializer<testContext>(new CreateDatabaseIfNotExists<testContext>());
策略二:每次启动应用程序时创建数据库
1 Database.SetInitializer<testContext>(new DropCreateDatabaseAlways<testContext>());
策略三:模型更改时重新创建数据库
1 Database.SetInitializer<testContext>(new DropCreateDatabaseIfModelChanges<testContext>());
策略四:从不创建数据库
1 Database.SetInitializer<testContext>(null);
Entity Framework数据库初始化示例
1 using System.Data.Entity; 2 using System.Data.Entity.Infrastructure; 3 using Web.Models.Mapping; 4 5 namespace Web.Models 6 { 7 public class testContext : DbContext 8 { 9 static testContext() 10 { 11 Database.SetInitializer<testContext>(null); 12 } 13 14 public testContext() 15 : base("Name=testContext") 16 { 17 } 18 19 public DbSet<Person> People { get; set; } 20 21 protected override void OnModelCreating(DbModelBuilder modelBuilder) 22 { 23 modelBuilder.Configurations.Add(new PersonMap()); 24 } 25 } 26 }