实体框架 - 无法创建引用同一个表的多个DbContexts。 - 解决方法

问题描述 投票:0回答:1

根据标题,场景是:一个c-sharp项目,有两个文件夹,每个文件夹包含两个EDMX上下文...

Myproj \ NSOne \ ModelOne.edmx,Myproj \ NSTwo \ ModelTwo.edmx;

通常这很有效,直到您在两个上下文中共享同一个表。在设计时没有问题,Entity Classes在不同的命名空间下生成,只有在运行时才会出现以下错误:

指定的架构无效。错误:CLR类型到EDM类型的映射是不明确的,因为多个CLR类型与EDM类型“TB_MyTable”匹配。以前发现CLR类型'Namespace.NSOne.TB_MyTable',新发现的CLR类型'Namespace.NSTwo.CIXModel.TB_MyTable'。

在寻找答案的过程中,我遇到了一个Github问题,它几乎说“太难/不可琐言”,我们无法修复。 https://github.com/aspnet/EntityFramework6/issues/362

但是,解决方法是编辑EDMX文件,如github问题中所述。

<ConceptualModels>
    <Schema Namespace="ConsoleApplication33" 
        Alias="Self" 
        annotation:UseStrongSpatialTypes="false"
        xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" 
        xmlns:customannotation="http://schemas.microsoft.com/ado/2013/11/edm/customannotation" 
        xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
    <EntityType Name="Person" 
        customannotation:ClrType="MyApp.Person, MyApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
        <Key>
        <PropertyRef Name="Id" />
        </Key>
        <Property Name="Id" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
        <Property Name="Name" Type="String" MaxLength="Max" FixedLength="false" Unicode="true" />
    </EntityType>
    <EntityContainer Name="Town" customannotation:UseClrTypes="true">
        <EntitySet Name="People" EntityType="Self.Person" />
    </EntityContainer>
    </Schema>
</ConceptualModels>

我的问题/问题是,在我的项目中,上面带有“ConceptualModels”的xml片段无处可寻?

我哪里开始看?在我的edmx文件中(通过visual studio),我只能看到,例如,

  • ModelOne.edmx ModelOne.Context.tt ModelOne.Designer.csharp ModelOne.edmx.diagram ModelOne.tt

EntityFramework.dll,EntityFramework.SqlServer.dll版本是:

文件版本:6.2.61023.0产品版本:6.2.0-61023

entity-framework-6 edmx ef-database-first
1个回答
0
投票

这不是一个真正的答案,但似乎微软已经放弃了EF数据库优先(EDMX),所以在提出问题两个月之后我的下一个选择是从我当前的项目中摆脱它。

在未来的项目中,我还将使用EF Code-first,因为VS仍然允许轻松生成强类型类。

© www.soinside.com 2019 - 2024. All rights reserved.