Today is a sad day. First, today I saw an EF exception saying that "The model that supports the" DomainContext "context has changed since the database was created." It's close to midnight, and I still see this error. This is the end of my career - (
I am sure that nothing has changed in the model, but an error has appeared. I tried to create a new migration, it came out empty:
public void Up() { } public void Down() { }
The use of this migration was not useful - the error persisted. I used the general sentence to set the initializer as null:
Database.SetInitializer<DomainContext>(null);
And that made the error go away when I access the database. But this bothers me a lot - whenever I try to start a migration through code, I again see a similar error:
var configuration = new Migrations.Configuration(); configuration.TargetDatabase = new DbConnectionInfo("correct connection string", "System.Data.SqlClient"); var migrator = new DbMigrator(configuration); migrator.Update();
The Exception throw looks like this: System.Data.Entity.Migrations.Infrastructure.AutomaticMigrationsDisabledException: The database cannot be updated according to the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to code-based migration, or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.
I upgraded to EF 6.1 (I used to be in version 6.0.2), but that didn't matter.
Another thing that bothers me is that I can trigger migrations through the Nuget Console:
Update-Database
It works perfectly and does not give any problems. But when I install the database initializer to automatically start the migration:
var initializer = new MigrateDatabaseToLatestVersion<DomainContext, Migrations.Configuration>(); Database.SetInitializer(initializer); var domainContext = new DomainContext(); domainContext.Database.Initialize(true);
It is not possible to update the database in accordance with the current model, because there are pending changes and automatic migration is disabled. Either write the pending model changes to code-based migration, or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.
The real question is: why does EF have different hashes for models when launched through the Nuget console and through the Migrations DB-Initialiser? How can I find out what is different (model from db-state)? And how to fix it, so I don't need to use hacks (assign null to db-initaliser)?