2016-11-08 54 views
5

我知道當你在類之間有多個關係時會使用反轉屬性。但是我在逆向屬性和外鍵屬性之間感到困惑,因爲它們都用於定義關係。實體框架中的逆向屬性和外鍵有什麼區別?

public class PrivilegeToDbOperationTypeMap : BaseEntity 
{ 
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity), Column(Order = 0)] 
    public int PrivilegeToDbOperationTypeMapId { get; set; } 

    [ForeignKey("privilegeLookup"), Column(Order = 1)] 
    [Index("IX_PrivilegeLookupId_DbOperationLookupId", 1, IsUnique = true)] 
    public int PrivilegeLookupId { get; set; } 

    [ForeignKey("dbOperationTypeLookup"), Column(Order = 2)] 
    [Index("IX_PrivilegeLookupId_DbOperationLookupId", 2, IsUnique = true)] 
    public int DbOperationLookupId { get; set; } 

    #region Navigation Properties 

    public PrivilegeLookup privilegeLookup { get; set; } 

    public DbOperationTypeLookup dbOperationTypeLookup { get; set; } 

    [InverseProperty("privilegeToDbOperationTypeMap")] 
    public ICollection<RoleToPrivilegeDbOperationTypeMap> roleToPrivilegeDbOperationTypeMaps { get; set; } 

    #endregion Navigation Properties 
} 

回答

8

外鍵屬性用於:

  1. 指示與給定的外鍵屬性

    // this is foreign key property with related "privilegeLookup" navigation property. Database column name will be PrivilegeLookupId 
    [ForeignKey("privilegeLookup"), Column(Order = 1)]  
    public int PrivilegeLookupId { get; set; } 
    // this is related navigation property 
    public PrivilegeLookup privilegeLookup { get; set; } 
    
  2. 導航屬性的名稱或說明外鍵屬性的名稱給定導航屬性:

    // this is foreign key property 
    public int PrivilegeLookupId { get; set; } 
    // this is navigation property with related foreign key property 
    [ForeignKey("PrivilegeLookupId")] 
    public PrivilegeLookup privilegeLookup { get; set; } 
    

當默認的EF代碼優先約定不適用或以不適合您的方式應用時,它非常有用。 Here您可以看到EF代碼優先約定的列表。當你需要指示類的導航屬性的在類與同一個外鍵的另一個導航屬性

逆屬性特性是使用。例如:

public class Student 
{ 
    public int StudentID { get; set; } 

    public Standard CurrentStandard { get; set; } 
    public Standard PreviousStandard { get; set; } 
} 

public class Standard 
{  
    public int StandardId { get; set; } 

    public ICollection<Student> CurrentStudents { get; set; } 
    public ICollection<Student> PreviousStudents { get; set; } 
} 

這裏我們有兩個類,每個類都有兩個導航屬性。我們的意圖是在表Student中有兩個外鍵,可能是CurrentStandardId和PreviousStandardId,而Standard類的導航屬性也與相同的外鍵(一對多關係)有關。但是,在這種情況下,如果沒有進一步的指導,EF不會意識到這一點 - 相反,它會創建外鍵。要引導它,我們必須使用逆屬性特性:

public class Standard 
{ 
    public int StandardId { get; set; } 

    // reference to the name of another navigation property in class Student 
    [InverseProperty("CurrentStandard")] 
    public ICollection<Student> CurrentStudents { get; set; } 

    // reference to the name of another navigation property in class Student 
    [InverseProperty("PreviousStandard")] 
    public ICollection<Student> PreviousStudents { get; set; } 
} 

現在EF理解我們的意圖,將只創建兩個外鍵,儘管這些名字就會不好。同時更改列名,我們可以使用外鍵屬性:

public class Student 
{ 
    public int StudentID { get; set; } 

    public int CurrentStandardId { get; set; } 
    public int PreviousStandardId { get; set; } 

    [ForeignKey("CurrentStandardId")] 
    public Standard CurrentStandard { get; set; } 

    [ForeignKey("PreviousStandardId")] 
    public Standard PreviousStandard { get; set; } 
} 

長話短說 - EF可以推斷基於代碼的慣例很多東西。但是,當它不能(例如當你在同一個類中有兩個外鍵) - 你必須使用你的問題中的屬性來幫助它。

+0

謝謝@Evk清除疑慮。 – IAmDineshL

+0

爲了使它完成,property' public ICollection CurrentStudents'也可以用'[ForeignKey(「CurrentStandardId」)]'註釋。這使得它看似接近'InverseProperty',但它只能引用一個原始屬性,而不是引用屬性,比如'CurrentStandard'。 –

相關問題