我試圖使用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