Entity Framework Code Only Error: the Model Backing the Context Has Changed Since the Database Was Created

Entity Framework Code Only error: the model backing the context has changed since the database was created

I found the answer in the comments on this post on Scott Guthrie's blog.

http://weblogs.asp.net/scottgu/archive/2010/08/03/using-ef-code-first-with-an-existing-database.aspx

For those who are seeing this exception:

"The model backing the 'Production' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance."

Here is what is going on and what to do about it:

When a model is first created, we run a DatabaseInitializer to do things like create the database if it's not there or add seed data. The default DatabaseInitializer tries to compare the database schema needed to use the model with a hash of the schema stored in an EdmMetadata table that is created with a database (when Code First is the one creating the database). Existing databases won’t have the EdmMetadata table and so won’t have the hash…and the implementation today will throw if that table is missing. We'll work on changing this behavior before we ship the fial version since it is the default. Until then, existing databases do not generally need any database initializer so it can be turned off for your context type by calling:

Database.SetInitializer<Production>(null);

The model backing the 'DataContext' context has changed since the database was created

This worked for me.

Go to Package Manager Console and Run - Update-Database -force

How to Fix 'The model backing the context has changed since the database was created.' in database first

Almost every time I see this error it's because the database schema and the schema the code is expecting is not synced up. At some point the schema changed and you did not reflect your changes in code.

For example, you have a db column Adjustment, which is a money type that allows null values. However, in your c# class, it is of type decimal, which does not accept null values. There are also some other properties that are either not present, or the types don't match up.

Good news is that fixing it should be really easy if you are using database-first. It's as simple as going to your .edmx file, right-clicking an empty area, and choosing "Update Model from Database".

Sample Image

From there you can add, update, or delete items found in your database. Once you save the model file VS will recreate your model classes, and it should be synced up.

Just a note: if you rename a db column, VS will not delete the old column name from your model, and will throw errors until you delete the column from your code model manually.

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

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.

The model backing the 'EFDbContext' context has changed since the database was created

Add

Database.SetInitializer<EFDbContext>(null);

To your Global.asax file to disable ef migrations.
It seems you have migrations enabled for some reason.

The model backing the Database context has changed since the database was created

Now it's:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer<YourDbContext>(null);
base.OnModelCreating(modelBuilder);
}

in your YourDbContext.cs file.

Getting Error The model backing the 'DBContext' context has changed since the database was created.

Code first migration will help you resolving the case.But to proceed in your way to initialize the database you must replace DbContext with DBContext(i.e with your context class) in the Database.SetInitializer function like this.

  public class DBContext:DbContext
{
public DBContext()
{
Database.SetInitializer<DBContext>(new DropCreateDatabaseAlways<DBContext>());
}
}

Hope this helps.



Related Topics



Leave a reply



Submit