Google

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

Written on:March 19, 2012
Comments
Add One

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.

56 Comments add one

  1. Aweklin says:

    Awesome post! It really helped me.

  2. Praveen says:

    Superb !!!! it works like a charm … Thank you

  3. Awek says:

    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 :)

  4. Ilya says:

    Thank you!!!

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

  6. shyjuvs says:

    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

  7. mvcnoob says:

    I can’t get this to work but I really need it! isn’t accessible (the type or namespace could not be found), any ideas??

  8. Do you have this namespace : System.Data.Entity ?

  9. Gopesh says:

    Worked for me. Thank you so much. I was looking for it desperately.

  10. Mahesh says:

    Excellent !! Worked for me.. I am using EF 5.0

  11. Thank you…. its worked for me…

  12. Vinay says:

    Superb………..
    but any budy explain how it work or what was the problem?

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

  14. Ben says:

    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.

  15. Patrikos says:

    Amazing post! Bless this man.

  16. Meir says:

    Thank you.

  17. Hugo says:

    Awesome! Thank youuu

  18. neha says:

    Hi,

    Not working for me :(
    please help me anyone.

  19. neha says:

    oh i am sorry i was making mistake there,it’s working 100% cool save me form many headaches :)

    Thanx a lot

  20. krupesh says:

    Thanks a lot it is working now

  21. Rik says:

    Good lord, thank you!!

  22. airik says:

    Thank you!!!!!
    it’s solved the problem.

  23. Prabhu says:

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

  24. prabhu says:

    Hi ,
    I got it …..

    Any way tx.

  25. Ranjith says:

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

  26. Jay says:

    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

  27. Alex says:

    Wow. Thanks a lot. Saved me a lot of time, actually!

  28. Twinkle says:

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

  29. Joel says:

    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

  30. Tirumala says:

    This worked like a charm..

  31. Murat says:

    Thanks you very much. It is really so simple and effective article for fixing the problem. Regards…

  32. Santosh says:

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

  33. BAsavaraja k says:

    Nice.. it saved my 4 hours task..
    Thanks

  34. Larry Morris says:

    Thank you. Saved me a lot of time.

  35. ehliel says:

    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)

  36. ehliel says:

    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 ***************/

  37. MD.Jahurul Islam says:

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

  38. revital says:

    You are a genius! thanks!!!!

  39. hari says:

    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

  40. Kristian says:

    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.

  41. Jorge says:

    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 Comment

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>