这是我想用实体框架映射的场景,从它看起来我相信它在SQL Server中是可能的,因此它可能在Entity Framework中是正确的吗?所以这里,
方案
操作由团队成员组成。两名团队成员很特别,他们是领导者和共同领导者。我希望这些直接附加到像这样的操作
public class Operation
{
public int OperationId { get; set; }
[ForeignKey("Leader")]
public int LeaderId { get; set; }
public TeamMember Leader { get; set; }
[ForeignKey("CoLeader")]
public int CoLeaderId { get; set; }
public TeamMember CoLeader { get; set; }
/// <summary>
/// Everybody in the operation
/// </summary>
public List<TeamMember> Team { get; set; }
}
团队成员看起来像这样:
/// <summary>
/// A team member is specific for an operation, and is the linking table
/// between people and an operation
///
/// This table cannot be a composite key of OperationId and PersonId,
/// as team members are created first and then the Person is assigned
/// to be a team member.
/// </summary>
public class TeamMember
{
public int TeamMemberId { get; set; }
[ForeignKey("Operation")]
public int OperationId { get; set; }
public Operation Operation { get; set; }
[ForeignKey("Person")]
public int PersonId { get; set; }
public Person Person { get; set; }
}
现在这几乎可以了。如果我从Operation
中删除导航属性TeamMember
,我会获得所需的数据库。但是使用导航属性我收到错误:
Unable to determine the principal end of the 'TeamMember_Operation'
relationship. Multiple added entities may have the same primary key.
所以现在有点狡猾的东西,你可以在查询teamMember.Operation.Leader.Operation.Leader
等时这样做....但你无论如何都可以在数据库查询中这样做,所以我想这不是错误的原因。
问题
为什么要使用相同的主键添加多个实体,以及如何解决此问题。
(如果可能,会更喜欢数据注释)