0

I would like to be able to have the database automatically upgraded by Entity Framework, however I also need to allow older versions to connect to the existing database so that I can do rolling upgrades without an outage.

I found this article which descriptions how to disable model compatibility checking, however this appears to be incompatible with the solution to automatically upgrade.

Entity Framework 6.1.1 disable model compatibility checking

I also found Database.CompatibleWithModel however this only seems to be true/false and does not indicate if the schema is less than or greater than the current version.

Here is my current code which allows me to rebuild the database (for debugging) or automatically upgrade (if out of date).

public MyContext() : base("name=MyDB")
{
        if (ConfigurationManager.AppSettings["RebuildDatabaseOnStart"] == "true")
            Database.SetInitializer(new DropCreateDatabaseIfModelChanges<MyContext>());
        else
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Migrations.Configuration>());
}

One approach I'm considering would be to find the DbMigrations in my assembly and then compare to migration history table but this seems like a "hack" rather than a solution.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Justin
  • 1,303
  • 15
  • 30
  • I have yet to find a use case where automatic migrations make sense in a production environment. – Eric J. Mar 12 '21 at 21:14
  • i should clarify, i am using manual migration definitions but using the automatic upgrade option. the database is extremely simple (~5 tables). – Justin Mar 13 '21 at 02:17

0 Answers0