I'm working on a project with EntityFramework and MVC, and I keep getting this error :
The model backing the 'EFDbContext' context has changed since the database was created.
The problem is that there are no migration pending (I've tried to delete/recreate the database, but I still get the error)
I've read several posts on the Web that let me think that I'm not the only one who struggle with this error.
Please find more details below. Any help will be welcome to understand the situation and fix it.
Project details
I've three projects in my solution:
- Core : Models and mappings with the database
- GUI.MVC : A web project (using the models from the Core)
- GUI.Console : A non-web project (using the same models)
I'm trying keep it "Code First" (I would like to work in the code only, and let EF deal with the database)
Step-by-step
I've deleted the database and the "Migrations" folder in the Core project to start "from scratch".
I run the GUI.MVC project : I get no error while browsing and the database is created "on-the-fly" when I access to the views based on it.
However, if I run the GUI.Console project, I get the error.
I open the Package Manager Console with the Core project as default project (in the dropdown list) and the MVC project (with the connexionstring in the Web.config) as startup project
I run the command
Enable-Migrations -EnableAutomaticMigrations. A new folder "Migrations" is created in the Core project. The situation is the same : the MVC project works, the Console doesn't.I run the command
Update-Database, and get :No pending explicit migrations. Applying automatic migration: 201408071410203_AutomaticMigration. Running Seed method.Now, the MVC project no longer works : I get the error (The model backing...) when I try to access the data. However, the Console project is now working!If I re-run the command
Update-Database, I getNo pending explicit migrations. Running Seed method.And I still get the error in the MVC project, and the Console project is working.
Configuration
Nuget packages
- Entity Framework 6.1.1
- ASP.NET MVC 5.1.2
Visual Studio 2013 Update 2
Thanks a lot !
Web.Release.configto be used, it isn't. - Chris PrattDatabase.SetInitializer(). By default EF Code First uses "create on first use"; I cant' remember if enabling migrations automatically switches to the "migrate to latest" for you, or if you have to do it manually, but you could check. - Michael Edenfieldcontext.Database.Connection.ConnectionString, wherecontextis a variable holding an instance of yourEFDBContextwithin your MVC project. Verify that this is what you expect it to be. - Chris Pratt