Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
619 views
in Technique[技术] by (71.8m points)

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

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

Create Model Builder:

private static DbModelBuilder CreateModelBuild()
{
    var builder = new DbModelBuilder();

    //add entity classes about 12 of them

    builder.Conventions.Remove<IncludeMetadataConvention>();
    return builder;
}

Create session:

private bool BuildSqlServerSession(DbModelBuilder builder)
{
    var model =
    builder.Build(new SqlConnection(@"connection string"));
    var cm = model.Compile();
    var context = new LittlePOSContext(cm);
    var dbExists = context.Database.Exists();
    _session = new EFSession(context);
    return dbExists;
}

This works when I run the code for first time. But when running on second time and trying to add an object using context.Add(myEntity) I get following exception:

Model compatibility cannot be checked because the EdmMetadata type was not 
included in the model. Ensure that IncludeMetadataConvention has been added 
to the DbModelBuilder conventions.

I have tried removing following line:

builder.Conventions.Remove<IncludeMetadataConvention>();

but I still get the error.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...