Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF 4.3 CodeFirst MVC3 WebApp and console use same model, but somehow they 'see' a different model which results in a model backing error

I'm using the same model for multiple applications: MVC3 web app, Windows services, and a console application. When I start the MVC3 web app, it generates the database. I can restart it, and everything is fine. But when I start the console application I get an error:

The model backing the '...Context' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).

Same occurs when I drop the database, start console app, I can restart this, and everything is fine too. When I start the MVC web app. Crash: model backing... etc.

In EF4.1 removing the EdmMeta table 'solved' the problem. But since EF4.3 doesn't have such a table anymore I can't fix it that way. I've checked that all apps refer to same model dll's. I've double checked that all projects reference to EF4.3 so that's not the cause of the problem.

Any constructive help would be appreciated.

Regards, Erwin van Dijk.

like image 200
Erwin van Dijk Avatar asked Dec 17 '22 03:12

Erwin van Dijk


1 Answers

You should not let multiple applications to create the database - that can result in unexpected deletion of your database. Simply choose one which will be responsible for database creation and in all other use:

Database.SetInitializer<YourContext>(null);

Also add this to your OnModelCreating in your derived DbContext:

modelBuilder.Conventions.Remove<IncludeMetadataConvention>();

This should avoid problems with hash computation.

More about reasons why problems with hash computation exist is described here.

like image 180
Ladislav Mrnka Avatar answered Dec 18 '22 16:12

Ladislav Mrnka