9.5 翻譯系列:數據注解之ForeignKey特性【EF 6 Code-First系列】


原文鏈接:https://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code-first.aspx

EF 6 Code-First系列文章目錄:

在EF 6和EF Core中,數據注解中的ForeignKey特性,是用來在兩個實體間配置外鍵關系。根據默認的約定,當屬性的名稱與相關實體的主鍵屬性匹配時,EF將該屬性作為外鍵屬性。ForeignKey Signature: [ForeignKey(name string)]
name:相關聯的導航屬性的名稱或者相關聯的外鍵屬性名稱
看看下面實體間的一對多關系:

using System.ComponentModel.DataAnnotations.Schema;

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    //Foreign key for Standard
    public int StandardId { get; set; }
    public Standard Standard { get; set; }
}

public class Standard
{
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    public ICollection<Student> Students { get; set; }
}

上面的代碼例子中,描述了Student和Standard實體中的一對多關系。為了解釋一對多關系,Student類包含了一個StandardId屬性還有一個引用類型的屬性Standard,並且Standard實體包含了一個集合類型的導航屬性Students,Student實體中的StandardId屬性匹配上了Standard實體中的主鍵屬性名稱StandardId,所以Student實體中的StandardId屬性將會自動變成外鍵屬性,並在數據表中生成外鍵:
enter description here
ForeignKey特性重寫了默認的外鍵約定,他允許我們在依賴實體中【這里是Student】指定外鍵屬性,這個指定的外鍵屬性名稱,不需要匹配主體實體【這里是Standard】中的主鍵屬性名稱。
使用ForeignKey數據注解特性,可以有以下三種方式:

  1. [ForeignKey(NavigationPropertyName)]應用在依賴實體的外鍵標量屬性上面,ForeignKey里面的name參數,填寫導航屬性的名稱
  2. [ForeignKey(ForeignKeyPropertyName)]應用在依賴實體的導航屬性上面,ForeignKey里面的name參數,填寫外鍵屬性的名稱
  3. [ForeignKey(ForeignKeyPropertyName)]應用在主體實體的導航屬性上面,ForeignKey里面的name參數,填寫外鍵屬性的名稱

1.[ForeignKey] on the foreign key property in the dependent entity

ForeignKey應用在依賴實體的外鍵屬性上面,相關聯的導航屬性的名稱作為ForeignKey的name參數傳入:

using System.ComponentModel.DataAnnotations.Schema;

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    [ForeignKey("Standard")]
    public int StandardRefId { get; set; }
    public Standard Standard { get; set; }
}

public class Standard
{
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    public ICollection<Student> Students { get; set; }
}

在上面的例子中,ForeignKey特性應用於StandardRefId屬性上,並且傳入導航屬性的名稱Standard到name參數上,這樣就會在Students表中創建一個外鍵列StandardRefId,這樣就不會生成默認的StandardID列了。
enter description here

2.[ForeignKey] on the navigation property in the dependent entity

ForeignKey特性可以應用在導航屬性上面,然后name參數就指定外鍵屬性列的名稱:

using System.ComponentModel.DataAnnotations.Schema;

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    public int StandardRefId { get; set; }
    
    [ForeignKey("StandardRefId")]
    public Standard Standard { get; set; }
}

public class Standard
{
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    public ICollection<Student> Students { get; set; }
}

在上面的例子中,ForeignKey特性應用在Standard導航屬性上面,name參數就是外鍵屬性的名稱StandardRefId,這樣就會在Students數據表中,生成一個StandardRefId外鍵列,就不會生成默認的StandardId列了。

3.[ForeignKey] on the navigation property in the principal entity

ForeignKey特性可以應用在主體實體的導航屬性上面,name參數就指定外鍵屬性的名稱:

using System.ComponentModel.DataAnnotations.Schema;

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    public int StandardRefId { get; set; }
    public Standard Standard { get; set; }
}

public class Standard
{
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    [ForeignKey("StandardRefId")]
    public ICollection<Student> Students { get; set; }
}

在上面的例子中,ForeignKey特性應用於主體實體Standard中的導航屬性Students上,這樣同樣會在Students表中創建一個外鍵列StandardRefId.
好了,以上就是數據注解之ForeignKey特性,大家有什么不明白可以留言,感謝支持!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM