[英]Using Data annotations to create navigation property from class to itself?
首先使用實體框架代碼,我有一個用於保存下拉列表數據的類。 同一類保存的記錄是主列表中項目的子項目。 最終,這將創建一組級聯的下拉列表。
我試圖弄清楚如何使類的導航屬性鏈接回自身。 問題類是我用來填充下拉列表的類。 投訴類還具有指向“問題”類的鏈接,但不需要指向該子類別的鏈接。
public class Issue
{
public Issue()
{
Complaints = new List<Complaint>();
SubIssues = new List<Issue>();
}
[Key]
public int IssueID { get; set; }
public string Name { get; set; }
public bool IsSubCategory { get; set; }
[ForeignKey("IssueID")]
public ICollection<Issue> SubIssues { get; set; }
public virtual ICollection<Complaint> Complaints { get; set; }
}
public class Complaint
{
public Complaint()
{
}
public int ComplaintID { get; set; }
public string Name {get; set;}
[ForeignKey("IssueID")]
public virtual Issue Issue { get; set; }
}
我做過類似的事情,但實際上在孩子中只有一個父母參考。 無論哪種方式,它都應該起作用。
public class Folder
{
[Key]
public int Id { get; set; }
// Some Property
public string Name { get; set; }
// They foreignkey for Many-side
public virtual Folder Parent { get; set; }
// The list for One-side (Not tested in my application)
public virtual ICollection<Folder> SubFolders { get; set; }
}
它與常規的一對多關系相同,只是所有引用都在同一實體內。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.