2016-06-08 118 views
4

我厌倦了使用/运行Add-MigrationUpdate-Database因为很多时候我们忘记在生产数据库上运行迁移。因此,我们决定删除数据库表中的所有迁移以及所有迁移类。那么,如果我的__MigrationHistory表始终为空并且没有迁移类,该怎么办?主要缺点是什么?缺点 - 使用EF 6.x.x代码首先没有迁移

回答

3

不,这是一个坏主意,[__MigrationHistory]用于比较您当前使用的概念模型和存储模型。

1)案例:DbMigrationsConfiguration自动:

[__MigrationHistory]跟踪最后迁移。

this.AutomaticMigrationsEnabled = true; 
    this.AutomaticMigrationDataLossAllowed = true; 
如上图所示

AutomaticMigrationInDb
,所述migrationId是迁移标识符和型号列是当前的概念模型的二进制流的一个base64表示。

2)情况:DbMigrationsConfiguration非自动:

[__MigrationHistory]保持跟踪每迁移。

this.AutomaticMigrationsEnabled = false; 
    this.AutomaticMigrationDataLossAllowed = false; 

每个迁移级别获取用于迁移级别up/level/down的migrationId标识符。

最佳实践:如果要使用自动迁移,只需重新生成迁移并发送给custmer。

如果您正在使用非自动迁移。 1)如果客户需要数据,那么只需将数据转换为新的数据库模式 2)如果客户不需要数据,则只需删除数据库并使用initialCreate再次创建数据库。


如果你想不带任何migrationHistory信息来创建数据库:

 // Create the database. You can generate it from SMO "Script database as" and if the database exist then just ignore the creation 
     // Do not forget to remove the [__MigrationHistory] rows. 
     DbContext.Database.ExecuteSqlCommand("SQLCreateDbScript.sql"); 

     Database.SetInitializer<MyDbContext>(null); 

     using (var dbContext = new MyDbContext()) 
     { 
      // Create DbContext and it works! 
      dbContext.Users.Add(new User()); 
      dbContext.SaveChanges(); 
     } 

我希望这会帮助你解决问题!

如果你想用Db-Schema更少的东西,那么在EF中使用NoSql。 核心版本还没有完成,但与旧版本EF6有可能待办事项是(的NoSql DB)与:http://www.brightstardb.com或Microsoft https://msdn.microsoft.com/en-us/library/dn271399.aspx

+0

来使用多语种appoarch上我只是不想要像很多其他的ORM的任何迁移。我们的经理讨厌'Db Schema Change Error'。我知道这是一个坏主意,但我根本不想移民。它使我们哭泣。我们现在不能。我们的第二个opton完全放弃了这个EF。 – user960567