EF 4 Code First: Model compatibility cannot be checked because the EdmMetadata type was not included in the model

前端 未结 3 1947
刺人心
刺人心 2020-12-11 01:49

I am trying to use EF 4 Code First pattern. My initialization code is as follows:

Create Model Builder:

private static DbModelBuilder CreateModelBuil         


        
相关标签:
3条回答
  • 2020-12-11 02:15

    Well it feels somewhat silly but the real culprit was following statement:

    Database.SetInitializer(new DropCreateDatabaseIfModelChanges<MyContext>());
    

    It seems that DropCreateDatabaseIfModelChanges is not compatible with Code First approach or it is some other mystery that I don't understand (yet).

    0 讨论(0)
  • 2020-12-11 02:21

    If you are getting this error in VS 2012 and are running in debug mode using IIS Express, try switching to Visual Studio Development Server.

    I was getting the same error, in a code-first asp.net mvc 4 application, using Visual Studio 2012 on Windows 7 machine, hitting SQL Server LocalDb (the server, no *.mdf), running on my local host in debug mode.

    My initializer is marked with the DropCreateDatabaseIfModelChanges attribute, and I am calling Database.Initialize in Global.ascx.cs Application_Start. If the database is being dropped and recreated how in the heck could it complain about a missing migration table? This was especially aggrating because I'd set up another web application using the same approach and it worked flawlessly.

    Even manually dropping the database before running the app did not fix the problem.

    Comparing the "good" app to the "bad" app, the only difference was the choice of web servers. The "good" app used Visual Studio Development Server; the "bad" used IIS Express.

    I switched the "bad" app to Visual Studio Development Server and the error disappeared!

    I don't have time to dig into the "why" right now, but apparently something incorrect is being cached by IIS Express.

    0 讨论(0)
  • 2020-12-11 02:36

    Removing IncludeMetadataConvention means the initializer cannot tell when the model changes. Adding it back in won't help either because it only creates the meta data table on db creation, which obviously won't exist for preexisting databases or databases that were created with the convention disabled.

    Solution is dropping the database and enabling the convention, or disabling the initializer and updating the database another way (manual or ef migrations)

    0 讨论(0)
提交回复
热议问题