问题
I've got database tables like this:
A person may be a member of many teams. A team may have many members. Each person may have a position (think job title) within the team.
I've tried to set this up with ADO.NET Entity Framework and get errors:
Error 3021: Problem in mapping fragments starting at line ... Each of the following columns in table Membership is mapped to multiple conceptual side properties: Membership.PersonId is mapped to <MembershipPerson.Membership.PersonId, MembershipPerson.Person.Id>
and
error 3021: Problem in mapping fragments starting at line ... Each of the following columns in table Membership is mapped to multiple conceptual side properties: Membership.TeamID is mapped to <MembershipTeam.Membership.TeamId, MembershipTeam.Team.Id>
The primary key of my Membership entity is a compound key of two foreign keys. I think that's the problem.
What must I do differently?
回答1:
This happens if you use independent association on the property which is both part of primary key and foreign key. EFv4 introduced Foreign key associations (the difference is described here) and once you expose foreign key in the entity you must define foreign key association. After defining referential constraints delete mapping of independent association in Mapping details window.
来源:https://stackoverflow.com/questions/5862352/entity-framework-compound-key-many-to-many-relationship-with-a-payload