2

这是我想与实体框架映射的场景,从外观上看,我相信在 SQL Server 中是可能的,因此在实体框架中是可能的,对吧?所以这里,

设想

一个操作由团队成员组成。两个团队成员很特别,他们是领导者和共同领导者。我希望这些直接附加到这样的操作

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; }
}

现在这几乎可以工作了。如果我从中删除导航属性OperationTeamMember我将获得所需的数据库。但是使用导航属性我得到错误:

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这样做等等......但你实际上可以在数据库查询中这样做,所以我认为这不是错误的原因。

问题

为什么要使用相同的主键添加多个实体,以及如何解决这个问题。

(如果可能,更喜欢数据注释)

4

0 回答 0