告诉我如何正确制定EF核心的数据库结构

时间:2019-02-23 12:08:53

标签: entity-framework-core

我正在使用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
} 

1 个答案:

答案 0 :(得分:0)

问题是基于观点的,取决于您的要求。我将仅使用两个类(Occam的剃刀)来实现您的逻辑,甚至可能不需要额外的枚举-您可以添加一个方法来检查一个FK是否具有值-我想这是最简单的方法。