首页 > 解决方案 > 告诉我如何正确制定 EF 核心的数据库结构

问题描述

我正在使用 EF 核心,需要建议,如何正确创建表。有文章,可以评论。也可以对评论本身发表评论。如何描述数据库的实体?

所以:

public class Article 
{
   public int Id {get; set;}
   public string Content {get; set;}

   public List<CommentForArticle>Comments{get;set;}       
}
public class CommentForArticle
{
   public int Id {get; set;}
   public string Content {get; set;}
   public List<CommentForComment> Comments {get; set;}

   public int ArticleId {get; set;}
   public Article Artcile {get; set;}
}
public class CommentForComment
{
   public int Id {get; set;}
   public string Content {get; set;}      

   public int CommentId {get; set;}
   public CommentForArticle CommentForArticle{get; set;}
}

或者:

public class Article
{
   public int Id {get; set;}
   public string Content {get; set;}

   public List<Comment>Comments{get; set;}       
}
public class Comment
{
   public int Id {get; set;}
   public string Content {get; set;}

   public CommentType Type {get; set;}

   public List<Comment> Comments {get; set;}

   public int? ArticleId {get; set;}
   public Article? Artcile {get; set;}
   public int? CommentId {get; set;}
   public Comment? Comment{get; set;}
}
public enum CommentType 
{
   CommentForArticle,
   CommentForComment
} 

标签: entity-framework-core

解决方案


问题是基于意见的,取决于您的要求。我将只用两个类(奥卡姆剃刀)来实现你的逻辑,甚至可能没有额外的枚举——你可以添加一个方法来检查一个并且只有 FK 有一个值——我想这是最简单的方法。


推荐阅读