Entity Framework creates empty migration but insists that my model is different
Asked Answered
B

2

7

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

Bandog answered 25/3, 2014 at 23:57 Comment(3)
Why don't you try a dummy change? Let say to add a field in a table, and see what does EF do when you request to create a migration, and of course later remove that field.Obligee
Tried that - in migration added just that field and nothing more. I'm currently stepping through EF code and I think I've found the cause for the error. Once I fix it, I'll publish a blog-post. Can be useful to other people.Bandog
@Guillelon found the problem - it was attribute [AllowHtml] applied on one of the model's properties.Bandog
B
6

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.

Bandog answered 28/3, 2014 at 3:14 Comment(4)
Just curious if you posted this info yet with the EF folks and if so what was their feedback.Lingo
@Kevin I actually was not able to recreate the same issue on a blank solution . So not reported anywhere.Bandog
This seems to be reported at entityframework.codeplex.com/workitem/2167; the comments mentions applying an empty migration to get the model state updated, which is added in 6.1. and not present in previous versions.Flanna
@Flanna The issue talks about updating to 6.1 and issue with indexes (which also happened to me in another project), but problem in question was not during the update and nothing to do with indexes.Bandog
W
0

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

Wilinski answered 24/7, 2016 at 13:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.