Search code examples
entity-framework-4.1database-first

EF 4.1 Code First with Existing Database


I have an existing DB and I have used the EF 4.1 code first to map my POCO objects to the tables. But I get the this error:

EF 4.1 Error Model compatibility cannot be checked because the EdmMetadata type

Eventhough I have added the OnModelCreating method it still gives me the same error:

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{     
    modelBuilder.Conventions.Remove<IncludeMetadataConvention>(); 
}

My POCO and DB Table are exactly identical.


Solution

  • It looks like the issue is that the database exists yet you are trying to create it using EF CodeFirst. If you drop the database, this will most likely run and it will create the database and the table. Here is a link to a person that had this issue and that is how they resolved it:

    http://forums.asp.net/t/1673379.aspx/1?Unable+to+generate+Edm+Metadata+table+at+runtime+from+EF+code+first+model

    If you wanted to use your existing database (in case you had other data in it), I think you need to modify what Code First expects. Here is a SO article on how to do this:

    Entity Framework CTP 4 - Code First Custom Database Initializer

    Make sure you read all the posts because even the ones not accepted as the answer have some great information on things you can do.