Entity Framework creates empty migration but insists my model is different

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(); // <<-- exception is thrown here 

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); // <<-- this throws exception 

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

+6
source share
2 answers

The cause of my problem was the [AllowHtml] attribute applied to one of the models. The problem arose after updating MVC to 5.1.1 and WebApi to 2.1.

I removed this attribute from the EF-Model and split and redesigned the database and the problem went away.

I wrote a blog post on how to debug such issues: http://tech.trailmax.info/2014/03/inside_of_ef_migrations/

I also think that this [AllowHtml] attribute is an error, I will create a reproducible solution and send an error report to EF-people.

Refresh . I could never reproduce the error. [AllowHtml] attribute of class properties has nothing to do with this. The magic glitch that was.

+6
source

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

There were no model changes at all! I made a dummy change, as @Guilleon advised, and created a working migration ... but that didn't help.

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

0
source

All Articles