![](/img/trans.png)
[英]How to rebuild a tree hierarchy from a closure table (Entity Framework Core 5)
[英]Entity Framework Core model with hierarchy of the same table
我使用 ASP.NET 核心 Web API 與 .NET 核心框架
我有一個名為Article
的表,其中包含一些屬性,並且我正在為該文章可能包含其他文章而苦苦掙扎。 像層次結構或某種“材料清單”。
我不想創建像SubArticle
這樣的額外表格,因為它可以嵌套多次。
我想過創建一個映射表ArticleHierarchy
,它也用於 M2M 關系但只有一個表,這可能嗎?
public class Article
{
[Key]
public int Id { get; set; }
public string Name{ get; set; }
public string SomeProperties { get; set; }
public virtual ICollection<ArticleHierarchy> ArticleHierarchies { get; set; }
}
public class ArticleHierarchy
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[ForeignKey(nameof(ParentArticle))]
public int ArticleParentId { get; set; }
[ForeignKey(nameof(ChildArticle))]
public int ArticleChildId { get; set; }
public virtual Article ParentArticle { get; set; }
public virtual Article ChildArticle { get; set; }
}
這是通往 go 的一種方式,如果是這樣,我的課程需要看起來如何?
還是有更好的方法來做到這一點?
謝謝
你必須修復關系,添加
public class Article
{
[Key]
public int Id { get; set; }
public string Name{ get; set; }
public string SomeProperties { get; set; }
public virtual ICollection<ArticleHierarchy> ParentArticleHierarchies { get; set;
public virtual ICollection<ArticleHierarchy> ChilsArticleHierarchies { get; set; }
}
public class ArticleHierarchy
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[ForeignKey(nameof(ParentArticle))]
public int ArticleParentId { get; set; }
[ForeignKey(nameof(ChildArticle))]
public int ArticleChildId { get; set; }
public virtual Article ParentArticle { get; set; }
public virtual Article ChildArticle { get; set; }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.