I needed to do this explicitly because I am using uber-context for migration, a superset of other migrations. Key bit:
var dbMigrator = new System.Data.Entity.Migrations.DbMigrator( new Lcmp.EF.Migrations.Migrations.Configuration()); dbMigrator.Update();
When sprinkling Elmah magazine, I use this, called from Application_Start (). Pieces of it are stolen from the ideas of others. I'm not sure if a blocked part with thread protection is needed.
public static int IsMigrating = 0; private static void UpdateDatabase() { try { if (0 == System.Threading.Interlocked.Exchange(ref IsMigrating, 1)) { try { // Automatically migrate database to catch up. Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(new Exception("Checking db for pending migrations."))); var dbMigrator = new System.Data.Entity.Migrations.DbMigrator(new Lcmp.EF.Migrations.Migrations.Configuration()); var pendingMigrations = string.Join(", ", dbMigrator.GetPendingMigrations().ToArray()); Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(new Exception("The database needs these code updates: " + pendingMigrations))); dbMigrator.Update(); Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(new Exception("Done upgrading database."))); } finally { System.Threading.Interlocked.Exchange(ref IsMigrating, 0); } } } catch (System.Data.Entity.Migrations.Infrastructure.AutomaticDataLossException ex) { Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(ex)); } catch (Exception ex) { Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(ex)); } }
Scott Stafford May 24 '12 at 9:50 a.m. 2012-05-24 21:50
source share