我知道當你在類之間有多個關係時會使用反轉屬性。但是我在逆向屬性和外鍵屬性之間感到困惑,因爲它們都用於定義關係。實體框架中的逆向屬性和外鍵有什麼區別?
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
}
謝謝@Evk清除疑慮。 – IAmDineshL
爲了使它完成,property' public ICollection CurrentStudents'也可以用'[ForeignKey(「CurrentStandardId」)]'註釋。這使得它看似接近'InverseProperty',但它只能引用一個原始屬性,而不是引用屬性,比如'CurrentStandard'。 –