Home » Ado.Net » Entity Framework » The model backing the context has changed since the database was created, EF4.3/5.0

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.

62 Responses so far.

  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. […] mvc.  I usually only happens when using databases I do not have control over.The CauseRead this very good page for more info.The SolutionJust add in this bit of code to your Application_Start() in […]

  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. umesh palkar says:

    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. Great ! thanks a lot.

  22. Rik says:

    Good lord, thank you!!

  23. airik says:

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

  24. Prabhu says:

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

  25. prabhu says:

    Hi ,
    I got it …..

    Any way tx.

  26. Ranjith says:

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

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

  28. Alex says:

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

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

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

  31. Tirumala says:

    This worked like a charm..

  32. Murat says:

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

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

  34. BAsavaraja k says:

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

  35. Larry Morris says:

    Thank you. Saved me a lot of time.

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

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

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

  39. revital says:

    You are a genius! thanks!!!!

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

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

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

  43. Joshi says:

    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…… :(

  44. Joshi says:

    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.

  45. d. ajee says:

    What is the type parameter – MyContext?

    Thanks1

  46. Bharat says:

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

  47. Meenu says:

    Thanks a lot. Saved my day!!!!

Leave a Reply

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=""> <s> <strike> <strong>