iAteABug_And_iLiked_it
iAteABug_And_iLiked_it

Reputation: 3795

The model backing the 'EFDbContext' context has changed error

Following the sports store tutorial in Pro Asp.Net MVC book, I have TWO projects in my Asp.Net MVC4 solution. One SportsStore.WebUI and one SportsStore.Domain , which has my Product class below

I added a Required attribute to Name in my model class

    //Product.cs class
    [Required]
    public string Name { get; set; }
    //..more properties, not important as I get the same error for all -
   because I added Required to all

When I run the project I get The model backing the 'EFDbContext' context has changed since the database was created. error.

I have checked on stackoverflow and MSDN

Solution 1: Didn't Work

Suggested solution on MSDN was to run Update-Database -Force . That doesn't work , I get

"Could not load assembly 'SportsStore.WebUI'. (If you are using Code First Migrations inside Visual Studio this can happen if the startUp project for your solution does not reference the project that contains your migrations. You can either change the startUp project for your solution or use the -StartUpProjectName parameter.)"

That is strange because A- I'm NOT using code first and B-The Domain project DOES reference WebUI project

Solution 2: Didn't Work

Then I tried Enable-Migrations in the PM console and I get

No context type was found in the assembly 'SportsStore.WebUI'.

I then tried changing the startup project to SportsStore.Domain ( which has my product class) - nothing, same error

Upvotes: 1

Views: 1805

Answers (2)

batman 74
batman 74

Reputation: 396

I also had this error. I had an existing asp.net mvc using EF 5.0 that was working fine.

I then created a console app with EF 5.0 that pointed to the same database. The asp.net mvc app started throwing the model backing exception.

I noticed there was a new table named "_MigrationHistory". To get the asp.net app working again, I renamed this table "_MigrationHistory_backup"

Upvotes: 1

Claies
Claies

Reputation: 22323

This problem is actually occurring because you didn't change the target for your invocation of the scaffolding. In the Package Manager Console, when you run any commands which target your Domain objects, the Default Project dropdown needs to be set to your Domain project. If you try to run migrations or database updates against your web project, they will fail.

Edit

Important is this line in the error

If you are using Code First Migrations inside Visual Studio this can happen if the startUp project for your solution does not reference the project that contains your migrations.

the error suggests "Could not load assembly 'SportsStore.WebUI' which means that's the project the migrations were put in, even though they should have gone in your Domain Project.

Upvotes: 1

Related Questions