我正在尝试使用 EF 4 Code First 模式。我的初始化代码如下:
创建模型生成器:
private static DbModelBuilder CreateModelBuild()
{
var builder = new DbModelBuilder();
//add entity classes about 12 of them
builder.Conventions.Remove<IncludeMetadataConvention>();
return builder;
}
创建 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;
}
这在我第一次运行代码时有效。但是当第二次运行并尝试使用
context.Add(myEntity)
添加对象时我得到以下异常: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.
我尝试删除以下行:
builder.Conventions.Remove<IncludeMetadataConvention>();
但我仍然收到错误。
最佳答案
嗯,感觉有点傻,但真正的罪魁祸首是以下声明:
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<MyContext>());
看来
DropCreateDatabaseIfModelChanges
与 Code First
不兼容方法或者是其他一些我不明白的谜(还)。
关于entity-framework-4 - EF 4 代码优先 : Model compatibility cannot be checked because the EdmMetadata type was not included in the model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8630755/