entity-framework-4 - EF 4 代码优先 : Model compatibility cannot be checked because the EdmMetadata type was not included in the model

标签 entity-framework-4 ef-code-first

我正在尝试使用 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>());

看来DropCreateDatabaseIfModelChangesCode 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/

相关文章:

entity-framework - 我可以使用 AutoMapper 将一个列表映射到另一已实例化对象的列表吗?

c# - 通过 WCF 数据服务插入新的父/子

linq - 使用 linq 匹配多个字段时优先考虑字段

asp.net - WCF REST 服务模板 40 和 JSON 格式的 Entity Framework 返回空响应

c# - EF Code First 约定允许 ICollection<T> 属性为空集合而不是 null?

asp.net - Entity Framework 4.0 附加对象图

c# - 检查实体是否在 Code First 中的其他实体中有引用

asp.net-mvc-3 - 从 MVC 应用程序中的 EF Code-First DbContext 类生成 SQL CE 数据库

entity-framework - 如何在代码优先的多对多关系中指定关系名称

c# - 保存不为其关系公开外键属性的实体时出错