Dis-Advantages of using EF 6.x.x Code First without Migration

一笑奈何 提交于 2020-01-03 16:50:06

问题


I am fed up of using/running Add-Migration and Update-Database because lot of time we forget to run migrations on production database. So, we decided to delete all migrations from database table as well all migration classes. So, what if my __MigrationHistory table remains always empty and no migration classes. What is the main disadvantage?


回答1:


No this is a bad idea, the [__MigrationHistory] is used to compare your current used conceptual model with the storage model.

1) Case: DbMigrationsConfiguration automatic:

[__MigrationHistory] keeps track for the last migration.

  this.AutomaticMigrationsEnabled = true;
  this.AutomaticMigrationDataLossAllowed = true;


as shown in the image above, the migrationId is the migration identifier and the model column is a base64 representation of the binary stream of your current conceptual model.

2) Case: DbMigrationsConfiguration non-automatic:

[__MigrationHistory] keeps track of every migration.

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

Each migration level obtain a migrationId identifier which is used for migration level up/level/down.

Best practice: If you want to use the automatic migration just regenerate the migration and shipped to the custmer.

If you are using non-automatic migration. 1) If the customer need the data then just tranform the data with SQL to the new db schema 2) If the customer does not need the data then just drop the database an create it again with initialCreate.


If you want to create the database without any migrationHistory info:

        // 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();
        }

I hope this will help you to solve the problem!

And if you want something really with less Db-Schema then use NoSql with EF. In Core version still not done but with the old version EF6 it is possible todo that (NoSql Db) with: http://www.brightstardb.com or use the Polyglot appoarch from microsoft https://msdn.microsoft.com/en-us/library/dn271399.aspx



来源:https://stackoverflow.com/questions/37695220/dis-advantages-of-using-ef-6-x-x-code-first-without-migration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!