我最近更改了一个应用程序,使用以下内容开发:
DropCreateDatabaseIfModelChanges<Context>
Run Code Online (Sandbox Code Playgroud)
使用:
public class MyDbMigrationsConfiguration: DbMigrationsConfiguration<GrsEntities>
{
public MyDbMigrationsConfiguration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
}
}
Run Code Online (Sandbox Code Playgroud)
在我的db上下文中,我有:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Tell Code First to ignore PluralizingTableName convention
// If you keep this convention then the generated tables will have pluralized names.
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
//set the initializer to migration
Database.SetInitializer(new MigrateDatabaseToLatestVersion<GrsEntities, MigrationConfig>());
}
Run Code Online (Sandbox Code Playgroud)
我在DbMigrationsConfiguration中使用AddOrUpdate扩展覆盖了种子(上下文),我刚刚在drop db(DropCreateDatabaseIfModelChanges)上播种时使用了Add.
我的困惑是,无论DbContext是否有任何更改,迁移都会在应用程序的每次启动时运行.每次启动应用程序(库通过服务运行)时,初始化程序都会像Seed一样运行.我的预期行为是检查是否需要迁移(在幕后检查模型是否与物理数据库匹配)然后更新任何新的/删除的表/列,并且只有在某些内容发生更改时才运行种子.
在我的测试种子每次运行,这是可行但看似效率低,并不是我所期望的.不幸的是,MSDN文档非常有限.
我是否完全滥用MigrateDatabaseToLatestVersion?有没有办法获得我期望的行为(即只有种子,如果有模型更改)或者我应该只更改我的种子方法,以期在每次应用程序启动时运行?
entity-framework ef-code-first ef-migrations entity-framework-4.3