The model backing the context has changed since the database was created, EF4.3/5.0

Entity Framework version 4.35.0 (and since 4.1) can throw the exception that the model and the database cannot be mapped correctly. In fact, EF is saying that the model has changed since the database has been created.

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

To resolve this problem, you will find on the Web a lot of solution but most of them work with previous version of Entity Framework 4.35.0.

The solution from this version is to use the static method SetInitializer and bind to the context a Null value. If you are working on a Web solution, the best position to write the code is in the Application_Start of your Global.asax.cs file.

protected void Application_Start() 
{
    AreaRegistration.RegisterAllAreas();
    RegisterRoutes(RouteTable.Routes);
    //...
    Database.SetInitializer<MyContext>(null);
}

Using this code remove the error and let you handle manually the creation of the database.

If you like my article, think to buy my annual book, professionally edited by a proofreader. directly from me or on Amazon. I also wrote a TypeScript book called Holistic TypeScript

67 thoughts on “The model backing the context has changed since the database was created, EF4.3/5.0

  1. Thank you very much for this. It fixed the problem I was having with publishing with windows azure. Everything worked well for a day or so, but then this error would come up and all I needed to do was run “update-database” from the migrations console. To my demise, the error would come back after a certain time. Would you care to explain why this is occurring? This seems to have fixed my problem, but I do not see why my problem was occurring in the first place.

    I’m using the latest EF and Windows Azure SQL.

    Thank you.

    • You may not have the issue before because you didn’t have any modification on the meta data of the model or those modifications were not affecting the current model (like adding fields). I can’t give you more detail about Windows Azure I am sorry. I am glad that I could have help you 🙂

  2. Pingback: How to fix The model backing the context has changed since the database was created error

  3. Thanks a lot man. I searched a day, there didn’t see much solution to avoid code first migration. Everybody was suggesting to migrate, that I don’t want. This is worked

  4. Hi Vinay,
    To answer your main question concerning what was the problem, it’s pretty simple. Entity Framework try by default to create the database and to modify tables if the schema has chanced. If you want to use Entity Framework but doesn’t want it to handle the creation of the tables (which could be managed by a DBA) you need to specify that you want not this creation. How it works, well, it’s simply replace the initializer with null so nothing is created.

  5. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you.

  6. Hi ,
    i am getting error at “MyContext” like – the type or namespace name Mycontext could not be found.
    Please help me… (:

  7. Thanks a mill man. it worked.

    For vb.net developers you can simply add all the databasecontext that are having this problem on your app_start method in the global.asax file like this;

    Database.SetInitializer(Of DatabaseContext)(Nothing)

    Regards

  8. I got the same error but i dint change anything in the model …i gt the output and i created records and when i compile,publish it once more ,the error.. plz help me..

  9. This solved a problem I was having with Azure. I still wish I understood why it was necessary since the model hadn’t changed.

    if, when deploying, I had “Use Code First” deselected, I’d get a warning the model had changed.
    If I had “Use Code First” selected, I’d get a message that
    “Column names in each table must be unique. Column name ‘FirstName’ in table ‘dbo.UserProfile’ is specified more than once.”

    Bizarre.

    Thanks though, at least I have something working as a starting point!

    Joel

  10. After that code this error occur “An error occurred while executing the command definition. See the inner exception for details. ” what to do next.

  11. This is the Massage :

    The model backing the ‘XYZ_Db’ context has changed since the database was created. Consider using Code First Migrations to update the database

    How to Wrote it Please in (VB)

  12. Find The Solution in VB

    if your DataBase Name is XYZ_DB

    then the Solution is
    1 – Open “Global.asax”

    2 – in Upper of file content write :
    Imports System.Data.Entity

    3 – in lower of file content write :

    Database.SetInitializer(New DropCreateDatabaseIfModelChanges(Of Models.XYZ_DB)())

    /************* Have Fun ***************/

  13. protected void Application_Start()
    {
    AreaRegistration.RegisterAllAreas();
    RegisterRoutes(RouteTable.Routes);
    RegisterGlobalFilters(GlobalFilters.Filters);
    Database.SetInitializer(null);
    }

    but this is problem

    An error occurred while executing the command definition. See the inner exception for details.

    • Try
      Database.SetInitializer(null);

      MainDB should be the name of your class that is dereived from DbContext

      Then you need to add the two using statements at the top of the file.

      using System.Data.Entity;

      and the other using that refers to your class

      Or put the cursor over Database and press control + . and the dropdown list will have the using you will need (can do that for your class as well).

  14. problem got solved, but i lost the data that i had in my database.
    i want to fix this problem without deleting the records in the database.

    any help plz let me know

  15. Hi Patrick, does this not just move the problem somewhere else? If your model is out of sync with the schema of your tables, then you will just get a different exception further down the line when you try to use the objects which relate to those tables?

    thanks.

    • It does not mean that your model and table are out of sync. It means that Entity Framework will not try to use it mechanism to compare entities and tables. You may want to not have Entity Framework handles the synchronization. For example, you may not have the access to create/alter table. The DBA team could handle the database while you code the entities. To be able to work this way, you have to set to null the database initializer.

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

  17. Through this fix the Compile time error (Run Successfully) even change or add the field into the database (existing table) but when we create or add new record data not saved. please help me…… 🙁

  18. Database.SetInitializer(null);

    or

    Database.SetInitializer(new DropCreateDatabaseIfModelChanges());

    i’m trying to fix the problem with above code but not fix it.
    Through this fix the Compile time error (Run Successfully) even change or add the new field into the database (existing table) but when we create or add new record data not saved. please help me…… 🙁
    please help.

  19. Using this i am not getting error but when i am trying to get data from database its giving zero result …Please help me out..

Leave a Reply to Jay Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.