Today is a sad day. First thing today I saw EF exception saying that "The model backing the 'DomainContext' context has changed since the database was created.". It is close to midnight and I still see this error. This is the end of my career -(
I'm pretty sure nothing has changed in the model, yet the error appeared. I have tried creating a new migration, it came out empty:
public void Up()
{
}
public void Down()
{
}
Applying this migration did not do any good - the error persisted. I've used common suggestion to set the initialiser to be null:
Database.SetInitializer<DomainContext>(null);
And it made the error go away when I access the database. But this bothers me very much - whenever I try to run migrations through code, I see similar error again:
var configuration = new Migrations.Configuration();
configuration.TargetDatabase = new DbConnectionInfo("correct connection string", "System.Data.SqlClient");
var migrator = new DbMigrator(configuration);
migrator.Update(); // <<-- exception is thrown here
The Exception throw looks like this: System.Data.Entity.Migrations.Infrastructure.AutomaticMigrationsDisabledException : Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.
I've updated to EF 6.1 (was on 6.0.2 before), but this made no difference.
Another thing that bothers me that I can run migrations through Nuget Console:
Update-Database
Runs fine and does not give any problems. But when I set DB initialiser to run migrations automatically:
var initializer = new MigrateDatabaseToLatestVersion<DomainContext, Migrations.Configuration>();
Database.SetInitializer(initializer);
var domainContext = new DomainContext();
domainContext.Database.Initialize(true); // <<-- this throws exception
Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.
The real question is why does EF has different hashes for models when running through Nuget console and through Migrations DB-Initialiser? How can I find out what is different (model from db-state)? And how to fix this, so I don't have to use hacks (assign null to db-initaliser)?