当主键由 2 个具有 entity framework 的键组成时,创建 1 对 1 关系

Create a 1 to 1 relation when the Primary Key consist of 2 keys with entity framework

我在 3 table 之间有一对一的关系。

父table有2个主键(复合键)。让我们将它们命名为 StudentId1 和 StudentId2。

与相关的 table 有什么关系,我会把

  [Key, ForeignKey("Student")]
    public int StudentId { get; set; }

我不止一个 属性!

我应该那样做吗?为相关 Table 将 StudentId1StudentId2 写为 Key 属性 没有意义。

相关Table:

[Key, ForeignKey("Student")]
public int StudentId1 { get; set; }

[Key, ForeignKey("Student")]
public int StudentId2 { get; set; }

传统样本:

public class Student
{
    public Student() { }

    public int StudentId { get; set; }
    public string StudentName { get; set; }

    public virtual StudentAddress StudentAddress { get; set; }

}



public class StudentAddress 
{
    [Key, ForeignKey("Student")]
    public int StudentId { get; set; }

    public virtual Student Student { get; set; }
}

Column(Order = X) 不应该吗?

public class Student
{   
    [Key, Column(Order = 0)]
    public int StudentId1 { get; set; }

    [Key, Column(Order = 1)]
    public int StudentId2 { get; set; }

    public virtual StudentAddress StudentAddress { get; set; }
}


public class StudentAddress 
{
    [Key, ForeignKey("Student"), Column(Order = 0)]
    public int StudentId1 { get; set; }

    [Key, ForeignKey("Student"), Column(Order = 1)]
    public int StudentId2 { get; set; }

    public virtual Student Student { get; set; }
}