Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
615 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 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...