在博客页面上,子类别将链接到主类别,并将是子类别的文章。你认为模型结构正确吗?
我将使用Asp.net核心和EntityFrameworkCore架构来开发它。
public class Category
{
public int Id { get; set; }
public string CategoryName { get; set; }
public bool IsActive { get; set; }
public ICollection<SubCategory> SubCategories { get; set; }
}
--------------------------------------
public class SubCategory
{
public int Id { get; set; }
public string SubCategoryName { get; set; }
public bool IsActive { get; set; }
public int CategoryId { get; set; }
public virtual Category Category { get; set; }
public virtual ICollection<Article> Articles { get; set; }
}
----------------------------------------------
public class Article
{
public int Id { get; set;}
public string ArticleTitle { get; set; }
public string ArticleContent { get; set; }
public DateTime PublishDate { get; set; }
public int AuthorId { get; set; }
public virtual Author Author { get; set; }
public int SubCategoryId { get; set; }
public virtual SubCategory SubCategory { get; set; }
}发布于 2020-05-25 19:42:40
另一种方法是将类别模型更改为Self-referencing relationship
public class Category
{
public int Id { get; set; }
public string CategoryName { get; set; }
public int? ParentId { get; set; }
public bool IsActive { get; set; }
public Category Parent { get; set; }
public ICollection<Category> SubCategories { get; set; }
public ICollection<ArticleCategories> ArticleCategories { get; set; }
}然后在Many-to-Many类和Article类之间创建Category关系
public class ArticleCategories
{
public int ArticleId { get; set; }
public int CategoryId { get; set; }
public Category Category { get; set; }
public Article Article { get; set; }
}文章模型
public class Article
{
public int Id { get; set;}
public string ArticleTitle { get; set; }
public string ArticleContent { get; set; }
public DateTime PublishDate { get; set; }
public int AuthorId { get; set; }
public virtual Author Author { get; set; }
public ICollection<ArticleCategories> ArticleCategories { get; set; }
}在这种情况下,任何Article都可以有许多Category,而任何Category都可以用于许多Articles。
https://stackoverflow.com/questions/62007995
复制相似问题