加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – “实体类型不是当前上下文模型的一部分”当项目包含多个ED

发布时间:2020-12-15 07:47:56 所属栏目:百科 来源:网络整理
导读:我首先使用数据库,我有一个如下所示的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
我首先使用数据库,我有一个如下所示的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?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读