实体框架错误3021:映射到多个概念侧属性

时间:2013-02-28 02:39:07

标签: entity-framework

我有一张PK AuthorID的作者表。

然后我有一个名为AuthoritativeAuthors的表,其中包含PK AuthorID

AuthoritativeAuthor.AuthorID是指向Authors.AuthorID的外键。 作者中的AuthorID可能会也可能不会出现在AuthoritativeAuthors中。

这是我得到的错误 Error 3021: Problem in mapping fragments starting at line 4365:Each of the following columns in table AuthoritativeAuthors is mapped to multiple conceptual side properties: AuthoritativeAuthors.AuthorID is mapped to <FK_AuthoritativeAuthors_Authors.AuthoritativeAuthor.AuthorID, FK_AuthoritativeAuthors_Authors.Author.AuthorID>

以下是edmx中似乎相关的所有代码。

存储模型

<EntityType Name="Authors">
    <Key>
        <PropertyRef Name="AuthorID" />
    </Key>
    <Property Name="AuthorID" Type="int" Nullable="false" />
    <Property Name="Name" Type="nvarchar" MaxLength="254" />
</EntityType>
<EntityType Name="AuthoritativeAuthors">
    <Key>
        <PropertyRef Name="AuthorID" />
    </Key>
    <Property Name="AuthoritativeAuthorID" Type="int" Nullable="false" />
    <Property Name="AuthorID" Type="int" Nullable="false" />
</EntityType>
<Association Name="FK_AuthoritativeAuthors_Authors">
    <End Role="AuthoritativeAuthor" Type="WCDBModel.Store.AuthoritativeAuthors" Multiplicity="0..1" />
    <End Role="Author" Type="WCDBModel.Store.Authors" Multiplicity="1" />
    <ReferentialConstraint>
        <Principal Role="Author">
            <PropertyRef Name="AuthorID" />
        </Principal>
        <Dependent Role="AuthoritativeAuthor">
            <PropertyRef Name="AuthorID" />
        </Dependent>
    </ReferentialConstraint>
</Association>

概念模型

<EntitySet Name="AuthorSet" EntityType="WCDBModel.Author" />
<EntitySet Name="AuthoritativeAuthorSet" EntityType="WCDBModel.AuthoritativeAuthor" />
<AssociationSet Name="FK_AuthoritativeAuthors_Authors" Association="WCDBModel.FK_AuthoritativeAuthors_Authors">
    <End Role="Author" EntitySet="AuthorSet" />
    <End Role="AuthoritativeAuthor" EntitySet="AuthoritativeAuthorSet" />
</AssociationSet>
<EntityType Name="Author">
    <Key>
        <PropertyRef Name="AuthorID" />
    </Key>
    <Property Name="AuthorID" Type="Int32" Nullable="false" />
    <Property Name="Name" Type="String" Nullable="true" />
    <NavigationProperty Name="AuthoritativeAuthors" Relationship="WCDBModel.FK_AuthoritativeAuthors_Authors" FromRole="Author" ToRole="AuthoritativeAuthor" />
</EntityType>
<Association Name="FK_AuthoritativeAuthors_Authors">
    <End Type="WCDBModel.Author" Role="Author" Multiplicity="1" />
    <End Type="WCDBModel.AuthoritativeAuthor" Role="AuthoritativeAuthor" Multiplicity="0..1" />
</Association>
<EntityType Name="AuthoritativeAuthor">
    <Key>
        <PropertyRef Name="AuthorID" />
    </Key>
    <Property Type="Int32" Name="AuthorID" Nullable="false" />
    <Property Type="Int32" Name="AuthoritativeAuthorID" Nullable="false" />
    <NavigationProperty Name="Author" Relationship="WCDBModel.FK_AuthoritativeAuthors_Authors" FromRole="AuthoritativeAuthor" ToRole="Author" />
</EntityType>

映射

<EntitySetMapping Name="AuthorSet">
    <EntityTypeMapping TypeName="IsTypeOf(WCDBModel.Author)">
        <MappingFragment StoreEntitySet="Authors">
            <ScalarProperty Name="Name" ColumnName="Name" />
            <ScalarProperty Name="AuthorID" ColumnName="AuthorID" />
        </MappingFragment>
    </EntityTypeMapping>
</EntitySetMapping>
<EntitySetMapping Name="AuthoritativeAuthorSet">
    <EntityTypeMapping TypeName="IsTypeOf(WCDBModel.AuthoritativeAuthor)">
        <MappingFragment StoreEntitySet="AuthoritativeAuthors">
            <ScalarProperty Name="AuthoritativeAuthorID" ColumnName="AuthoritativeAuthorID"/>
            <ScalarProperty Name="AuthorID" ColumnName="AuthorID" />
        </MappingFragment>
    </EntityTypeMapping>
</EntitySetMapping>
<AssociationSetMapping Name="FK_AuthoritativeAuthors_Authors" TypeName="WCDBModel.FK_AuthoritativeAuthors_Authors" StoreEntitySet="AuthoritativeAuthors">
    <EndProperty Name="AuthoritativeAuthor">
        <ScalarProperty Name="AuthorID" ColumnName="AuthorID" />
    </EndProperty>
    <EndProperty Name="Author">
        <ScalarProperty Name="AuthorID" ColumnName="AuthorID" />
    </EndProperty>
    <Condition ColumnName="AuthorID" IsNull="false" />
</AssociationSetMapping>

任何协助查找此错误的原因将不胜感激。

1 个答案:

答案 0 :(得分:0)

我最终在AuthoritativeAuthor表中创建了一个虚拟主键,它不是外键。