c# – “实体类型不是当前上下文模型的一部分”当项目包含多个EDMX文件时抛出错误

前端之家收集整理的这篇文章主要介绍了c# – “实体类型不是当前上下文模型的一部分”当项目包含多个EDMX文件时抛出错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我首先使用数据库,我有一个如下所示的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();
}

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

如果我删除“两个”EDMX文件并注释掉条件,那么OneContext可以正常工作.如果我将TwoContext EDMX文件添加到项目并再次运行代码,则“OneContext”代码在尝试查询OrganizationObject时失败.

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

解决方法

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?

原文链接:https://www.f2er.com/csharp/99576.html

猜你在找的C#相关文章