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.