Question

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)?

Was it helpful?

Solution

The cause to my problem was a [AllowHtml] attribute applied to one of the models. The trouble started to happen after I've updated MVC to 5.1.1 and WebApi to 2.1.

I've removed that attribute from EF-Model and stripped and re-build the database and the problem was gone.

I've written up a blog post on how to debug this kind of problems: http://tech.trailmax.info/2014/03/inside_of_ef_migrations/

Also I think this [AllowHtml] attribute is a bug, I'll create a reproducible solution and will submit the bug report to EF-people.

Update: I actually could never reproduce the error. [AllowHtml] attribute on class properties had nothing to do with it. A magical glitch that was.

OTHER TIPS

I had the same problem today. I added before the problem appeared a ViewModel, a View and Html.EnableClientValidation()

There was no Model changes at all! I did a dummy change, like @Guilleon advised, and created a working Migration... but it didn't help.

Then I restarted the Visual Studio and everything worked again. It must be a glitch

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top