September 30, 2020
error-bug

Context Has Changed Since the Database Was Created

At present Entity Framework is used widely. We face some problems during working with Entity Framework. It takes lot of times to find out the perfect solutions. This article describes and provides the solutions of a common error in Entity Framework: Context has changed since the database was created, Model backing a DB Context has changed, Model backing the context has changed since the database was created.

When we work with Microsoft Entity Framework sometimes we get the bellow error:

The model backing the ‘MyDBContext’ context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).

Context has changed since the database was created

This is the most frequent error in Entity Framework ASP.NET MVC. This type of error occurred when the model and the database cannot be mapped correctly. That means there is a modification or changed in the database. We made any type of schema change on the table or database after including Entity Data Model in the project or solutions.

To resolve this problem there is lot of solution in the web. But most of them work with previous version of Entity Framework.

To solve this error writhe the following code in Application_Start() Method in Global.asax.cs file

Database.SetInitializer<CustomerDBContext>(null);

That means we need to bind our DB context class with a null value.

If the database schema is changed, it is mandatory to updates the .edmx file.

Rashedul Alam

I am a software engineer/architect, technology enthusiast, technology coach, blogger, travel photographer. I like to share my knowledge and technical stuff with others.

View all posts by Rashedul Alam →

10 thoughts on “Context Has Changed Since the Database Was Created

    1. Database.SetInitializer(null);

      can you plz elabroate this syntax ??
      ‘database ‘ does not exist in current context
      ‘CustomerDBContext’ could not be found
      i am getting error.

      i am new to this so……

      1. ‘Database’ comes from the System.Data.Entity library, you probably need to add a using statement to this library.

        ‘CustomerDBContext’ refers to your applications DB Context. I believe the default is ApplicationDbContext, so you may just need to change it to this. You can check what is setup in your Model under the IdentityModels.cs code.

        This is what resolved the issue for me.

  1. Thank you for this post, it helped me a lot.

    I have a few concerns though, that I would you’re willing to answer. When you say this error occurs when the model and the database can’t be mapped correctly, doesn’t this solution just hide the real problem – or does this really solve the deeper problem? If it doesn’t, is there any way get some insight into where the mapping fails, instead of that terribly generic exception EF6 spits out?

    Hope you can help.

    Best Regards
    – Nikolaj

  2. Update EntityFramework to last version disponible.
    Add the latest version of the EntityFramework NuGet package to the project

    Tools –> Library Package Manager –> Package Manager Console
    Run the Install-Package EntityFramework command

Leave a Reply

Your email address will not be published. Required fields are marked *