4

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?

Imran Qadir Baksh - Baloch
  • 32,612
  • 68
  • 179
  • 322

1 Answers1

3

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;

AutomaticMigrationInDb
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

Bassam Alugili
  • 16,345
  • 7
  • 52
  • 70
  • Come on I just dont wanna any migration like lot of other ORM. Our Managers hates `Db Schema Change Error`. I know its a bad idea but I dont want migration at all. It makes us cry. We can't it now. Second opton we have is drop this EF at all. – Imran Qadir Baksh - Baloch Jun 08 '16 at 09:20