我试图使用EF 4代码优先模式。我的初始化代码如下:EF 4代码优先:模型兼容性无法检查,因为EdmMetadata类型未包含在模型中
创建模型创建:
private static DbModelBuilder CreateModelBuild()
{
var builder = new DbModelBuilder();
//add entity classes about 12 of them
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)
我得到以下异常对象时:
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>();
,但我仍然得到错误。
它是兼容的。问题是,第一次创建它时,它没有用于跟踪模型更改的表格,因此它会抱怨。一旦数据库第一次由EF创建,您可以将初始化策略改回原来的状态。 – 2012-04-21 14:05:32