EF 4代码首先:由于EdmMetadata类型未包含在模型中,不能检查模型兼容性 [英] EF 4 Code First: Model compatibility cannot be checked because the EdmMetadata type was not included in the model
问题描述
创建模型构建器:
private static DbModelBuilder CreateModelBuild()
{
var builder = new DbModelBuilder();
//添加实体类约12个
builder.Conventions.Remove& IncludeMetadataConvention>();
return builder;
}
创建会话:
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)
添加一个对象时,我得到以下异常:
模型兼容性无法检查,因为模型中包含EdmMetadata类型不是
。确保将IncludeMetadataConvention添加到
到DbModelBuilder约定。
我尝试删除以下行:
builder.Conventions.Remove< IncludeMetadataConvention>();
但我仍然收到错误。
感觉有点傻,但真正的罪魁祸首是:
数据库。 SetInitializer(新的DropCreateDatabaseIfModelChanges< MyContext>());
似乎 DropCreateDatabaseIfModelChanges
与 Code First
方法或者是其他一些我不明白的林业(还)。
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.
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 mystry that I don't understand (yet).
这篇关于EF 4代码首先:由于EdmMetadata类型未包含在模型中,不能检查模型兼容性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!