"the entity type is not part of the model for the current context" 当项目包含多个 EDMX 文件时抛出错误

"the entity type is not part of the model for the current context" error is thrown when project contains more than one EDMX file

我首先使用数据库,我有一个 switch 语句,看起来像这样:

switch (site)
{
    case Site.One:
        using (OneContext one = new OneContext())
            return one.OrganizationObjects.SingleOrDefault(x => x.u_Name == orgName)?.g_org_id;
    case Site.Two:
        using (TwoContext two = new TwoContext())
            return two.OrganizationObjects.SingleOrDefault(x => x.u_Name == orgName)?.g_org_id;
    default:
        throw new NotImplementedException();
}

这两个数据库非常相似,并且具有几乎所有相同的模型。

如果我删除 "Two" EDMX 文件并注释掉该条件,那么 OneContext 将完美运行。
如果我将 TwoContext EDMX 文件添加到项目并再次 运行 代码,"OneContext" 代码在尝试查询 OrganizationObjects.

时失败

我确保每个上下文都使用了正确的连接字符串,但仍然出现此错误:

Entity Framework 匹配 class 名称和 class 属性。具有相同 class 名称和相同属性的两个 class 会导致冲突。

在两个相同的 class 之一上更改 属性 将解决问题。

Workaround: Change a property on one of the two identical classes.

EF matches on class name AND class properties. So I just changed a property name on one of the EF objects, and the error is gone.

As @Entrodus commented on one of the other answers:

EF collision happens only when two classes have the same name AND the same set of parameters.

The mapping of CLR type to EDM type is ambiguous with EF 6 & 5?