我試圖用CodeFirst生成我的數據庫指向。實體框架註釋與兩個表互相
我有兩個表Staff
和Team
,每個團隊中有一個團隊負責人是員工ID的外鍵,每個員工都與一個團隊相關聯。
public class Staff
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
public string Username { get; set; }
public string PasswordHash { get; set; }
public string Salt { get; set; }
public string Token { get; set; }
public string Mobile { get; set; }
public string Email { get; set; }
public bool Admin { get; set; }
public bool Active { get; set; }
public int TeamID { get; set; }
[ForeignKey("TeamID")]
public Team Team { get; set; }
}
public class Team
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
public int TeamLeaderID { get; set; }
[ForeignKey("TeamLeaderID")]
public Staff TeamLeader { get; set; }
}
因爲每一個指向其他的我得到一個錯誤Unable to determine the principal end of an association between the types 'Team' and 'Staff'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
如何詮釋它以這樣一種方式,它理解爲什麼我這樣做。
我建議改變球隊的TeamLeaderID porperty類型爲int? (可空int)。然後,您可以創建一個沒有TeamLeader的Team,並稍後設置TeamLeader。 – jannagy02