EF 6 Code-First系列文章目錄:
- 1 翻譯系列:什么是Code First(EF 6 Code First 系列)
- 2.翻譯系列:為EF Code-First設置開發環境(EF 6 Code-First系列)
- 3.翻譯系列:EF Code-First 示例(EF 6 Code-First系列)
- 4.翻譯系列:EF 6 Code-First默認約定(EF 6 Code-First系列)
- 5.翻譯系列:EF 6中數據庫的初始化(EF 6 Code-First 系列)
- 6.翻譯系列:EF 6 Code-First中數據庫初始化策略(EF 6 Code-First系列
- 7.翻譯系列:EF 6中的繼承策略(EF 6 Code-First 系列)
- 8.翻譯系列: EF 6中配置領域類(EF 6 Code-First 系列)
- 9.翻譯系列:EF 6以及EF Core中的數據注解特性(EF 6 Code-First系列)
- 9.1 翻譯系列:數據注解特性之----Table【EF 6 Code-First 系列】
- 9.2 翻譯系列:數據注解特性之---Column【EF 6 Code First系列】
- 9.3 翻譯系列:數據注解特性之Key【EF 6 Code-First 系列】
- 9.4 翻譯系列:EF 6以及 EF Core中的NotMapped特性(EF 6 Code-First系列)
- 9.5 翻譯系列:數據注解之ForeignKey特性【EF 6 Code-First系列】
- 9.6 翻譯系列:數據注解之Index特性【EF 6 Code-First系列】
- 9.7 翻譯系列:EF數據注解特性之--InverseProperty【EF 6 Code-First系列】
- 9.8 翻譯系列:數據注解特性之--Required 【EF 6 Code-First系列】
- 9.9 翻譯系列:數據注解特性之--MaxLength 【EF 6 Code-First系列】
- 9.10 翻譯系列:EF數據注解特性之StringLength【EF 6 Code-First系列】
- 9.11 翻譯系列:數據注解特性之--Timestamp【EF 6 Code-First系列】
- 9.12 翻譯系列:數據注解特性之ConcurrencyCheck【EF 6 Code-First系列】
- 10.翻譯系列:EF 6中的Fluent API配置【EF 6 Code-First系列】
- 10.1.翻譯系列:EF 6中的實體映射【EF 6 Code-First系列】
- 10.2.翻譯系列:使用Fluent API進行屬性映射【EF 6 Code-First】
- 11.翻譯系列:在EF 6中配置一對零或者一對一的關系【EF 6 Code-First系列】
- 12.翻譯系列:EF 6 中配置一對多的關系【EF 6 Code-First系列】
- 13.翻譯系列:Code-First方式配置多對多關系【EF 6 Code-First系列】
- 14.翻譯系列:從已經存在的數據庫中生成上下文類和實體類【EF 6 Code-First系列】
- 15.翻譯系列:EF 6中的級聯刪除【EF 6 Code-First 系列】
- 16.翻譯系列:EF 6 Code -First中使用存儲過程【EF 6 Code-First系列】
- 17.翻譯系列:將Fluent API的配置遷移到單獨的類中【EF 6 Code-First系列】
- 18.翻譯系列:EF 6 Code-First 中的Seed Data(種子數據或原始測試數據)【EF 6 Code-First系列】
- 19.翻譯系列:EF 6中定義自定義的約定【EF 6 Code-First約定】
- 20.翻譯系列:Code-First中的數據庫遷移技術【EF 6 Code-First系列】
- 20.1翻譯系列:EF 6中自動數據遷移技術【EF 6 Code-First系列】
- 20.2.翻譯系列:EF 6中基於代碼的數據庫遷移技術【EF 6 Code-First系列】
- 21.翻譯系列:Entity Framework 6 Power Tools【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屬性將會自動變成外鍵屬性,並在數據表中生成外鍵:
ForeignKey特性重寫了默認的外鍵約定,他允許我們在依賴實體中【這里是Student】指定外鍵屬性,這個指定的外鍵屬性名稱,不需要匹配主體實體【這里是Standard】中的主鍵屬性名稱。
使用ForeignKey數據注解特性,可以有以下三種方式:
- [ForeignKey(NavigationPropertyName)]應用在依賴實體的外鍵標量屬性上面,ForeignKey里面的name參數,填寫導航屬性的名稱
- [ForeignKey(ForeignKeyPropertyName)]應用在依賴實體的導航屬性上面,ForeignKey里面的name參數,填寫外鍵屬性的名稱
- [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列了。
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特性,大家有什么不明白可以留言,感謝支持!