我有一張PK AuthorID
的作者表。實體框架錯誤3021:映射到多個概念側屬性
然後我有一張名爲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>
在找到這個錯誤的原因的任何幫助,將不勝感激。
您使用的是什麼版本的實體框架,3.5或4以上? – 2013-07-05 01:08:28
[已回答] [1]在同一[問題] [2]中,希望能對你有所幫助。 [1]:http://stackoverflow.com/a/28759761/1638622 [2]:http://stackoverflow.com/questions/25177720/entityframework-mapping-issue/28759761#28759761 – 2015-02-27 08:25:16