I asked a question a while back about seamless updates:
Seamless EF Migrations from Staging > Production with Schema Change
I think a simple way to achieve this is to simply suppress the error The model backing the 'ApplicationDbContext' context has changed
- this way I can
- Publish my app to a staging slot
- Test migration against a staging db
- Swap staging/production
- Migrate directly to Prod
This is what I do now, but between 3
and 4
, there are a few seconds where the dbContext throws that error. If I could simply suppress that error... we could eliminate 95% of those errors across those few seconds (the 5% being errors that we catch resulting from the drifted model).
Is there a way to suppress this error but keep migrations enabled?
Update:
Using the link here I was able to test out the following:
public ApplicationContext()
: base("name=DefaultConnection") {
this.Database.Log = s => System.Diagnostics.Debug.WriteLine(s);
Database.SetInitializer(new SuppressedInitializer<ApplicationContext>());
}
public class SuppressedInitializer<TContext> : IDatabaseInitializer<TContext>
where TContext : DbContext {
public void InitializeDatabase(TContext context) {
if (!context.Database.Exists()) {
throw new ConfigurationException(
"Database does not exist");
}
else {
if (!context.Database.CompatibleWithModel(true)) {
var contextException = new InvalidOperationException("The database is not compatible with the entity model.");
Elmah.ErrorSignal.FromCurrentContext().Raise(contextException);
// intentionally don't throw so that the application may continue
}
}
}
}
contextException
is created when I expect it to and I believe this is a solution.