The model backing the '--Context' context has changed since the database was created - but db is new production database

azure c# entity-framework

Question

I've got this error for the 762nd time but this time I am getting it as soon as I attempt to access my Production site, straight after deleting the 'production' database on Azure and then publishing my site.

The model backing the 'PropertyContext' context has changed since the database was created. Consider using Code First Migrations to update the database

I deleted the database because I couldn't fix this issue any other way but it still doesn't work.

Some important points:

  • I'm using EF6 and publishing to Azure.
  • This is 1 of 2 projects/sites that uses the same Repo project. I have no
    problems with the other one, just this one.
  • I have tried publishing the problem project first (after deleting the db) and second with the same result.
  • I have tried deleting both WEBSITES and the DB from Azure and starting again
  • I have tried deleting all migrations and starting with a fresh data model
  • I have tried the following in my Global.asax (in both projects)

    Database.SetInitializer PropertyContext>(null); <-- SO won't let me put the first <

and

Database.SetInitializer(new MigrateDatabaseToLatestVersion<PropertyContext, MyConfiguration>());
new PropertyContext().Database.Initialize(true);

I'm using .net 4.5

Why am I getting this error on a new database and how can I get this site to work?

1
24
2/19/2014 8:16:56 AM

Accepted Answer

Just ran into the same error in ASP.Net application. In my case I did not use Code First, but I used standard ASP.Net authentication provider which apparently uses Code First, and authentication was broken because of this issue.

Here is quick and dirty solution is you don't care much about existing user records:

For me the solution was to drop the dbo.__MigrationHistory table, authentication started working fine after that. Be aware! This solution is not for everyone! This will fix the problem, but it is potentially risky.

If you cannot afford to lose data in AspNet* tables:

ASP.Net authentication provider automatically creates tables in your database:

  • AspNetRoles
  • AspNetUsers
  • AspNetUserRoles
  • AspNetUserClaims
  • AspNetUserLogings

The tables are empty by default, if you haven't created any new logins for your web site, you can use "quick and dirty" solution above. If you do care about preserving user information or just curios how Code First migrations work, follow these steps:

  • Open your Web.config file and check the name of the connection string you have for your database. It will be one of the records under <connectionStrings> element.
  • Open Package Manager Console:

    Tools –> Library Package Manager –> Package Manager Console

  • In Package Manager Console window, use a drop-down to set Default Project. Make sure this is the project that contains ASP.Net authentication provider code.
  • Execute command:
    Update-Database -ConnectionStringName MyConnectionStringName

Replace the MyConnectionStringName with the actual name you looked up in web.config.

As a result of this command you will see a new folder "Migrations" with a bunch of code generated by the Update-Database command. Re-build and re-deploy your app, your new migration code will be executed on startup and would bring the database schema in sync with an updated version of ASP.Net authentication provider code.

36
1/15/2016 4:56:46 PM

Popular Answer

When using Code First with Migrations, your database creates a table called __MigrationHistory to track the current schema. When you run your application your Entity Framework will check this table to make sure that the database schema matches your database entities. If they do not match, you will get this error.

To update your database follow these steps:

  1. Open the Package Manager Console (View -> Other Windows -> Package Manager Console) in Visual Studio
  2. In the Package Manager Console Window, there is a drop down with your projects in, make sure it is set to the project that contains your DbContext
  3. Make sure that the project that contains your App.Config / Web.Config file is "Set as Startup Project" (If you have multiple Configs, it must be the one with the Database Connection String defined.
  4. Type Update-Database -ConnectionStringName MyConnString where MyConnString is the name (not the actual connection string) of your connection string in your App.Config / Web.Config

If you get an error like this: "Unable to update database to match the current model because there are pending changes and automatic migration is disabled."

You should enable Automatic Migrations and try again. To enable Automatic Migrations

  1. In the Migrations folder (in the project with your DbContext), open Configuration.cs.
  2. Make sure the Constructor contains: AutomaticMigrationsEnabled = true;

To stop Entity Framework/DbContext from monitoring changes on your database you could simply delete the __MigrationHistory table in your database. It is then up to you to make sure that the database remains updated manually.

MSDN article here



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow