これを私に説明してください。私たちは外部キーとして従属テーブルの主キーを使用する理由を理解したいEF:1対1の関係にある外部キーですか?
public class Student
{
public Student() { }
public int StudentId { get; set; }
public string StudentName { get; set; }
public virtual StudentAddress Address { get; set; }
}
public class StudentAddress
{
[ForeignKey("Student")]
public int StudentAddressId { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public int Zipcode { get; set; }
public string State { get; set; }
public string Country { get; set; }
public virtual Student Student { get; set; }
}
、なぜ私たちは通常の方法に従わない:この例を見てみましょうを参照する従属テーブルの外部列を追加しますプリンシパルテーブルからのプライマリキー? そして、私たちはここで役割を逆転させることができます。つまり、Studentエンティティの主キーを外部キーとして使用できますか?主なテーブルの主キーを参照 従属テーブルの外部列を追加する:
XとYの間のタイプ1-1の関係は、1対1の関係の定義によって、XIdがXの主キーであり、Yの主キーとYの外部キーであることを意味します。 – Monah