1
votes

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

  1. I've deleted the database and the "Migrations" folder in the Core project to start "from scratch".

  2. 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.

  3. However, if I run the GUI.Console project, I get the error.

  4. 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

  5. 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.

  6. 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!

  7. If I re-run the command Update-Database, I get No 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 !

1
which database initializer are you using for your context? Can you confirm that both projects are, in fact, creating and using the same context class? - Michael Edenfield
Update-Database -Force - Zakos
Sounds like your MVC project is not using the database you think it is. Check your connection strings. Also, it's probably not the case, but be advised that transforms are not applied when debugging. So for example if you switched to "Release" config and expected the connection string in Web.Release.config to be used, it isn't. - Chris Pratt
@Randrelov normally you would have to override the initializer in your DbContext's static constructor, calling Database.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 Edenfield
Use the debugger to see, log to a file, or just print somewhere on your site the value of context.Database.Connection.ConnectionString, where context is a variable holding an instance of your EFDBContext within your MVC project. Verify that this is what you expect it to be. - Chris Pratt

1 Answers

0
votes

You can turn on debugger, i would assume the exception will stop at one the dbcontext or dbset, and inspect the dbcontext or dbset variable, find its dbcontext object , and inspect base -> Database -> connectionstring property, then you should see which database it is complaining. Looks like you are deleting the wrong database.