Tell me how to correctly formulate the database structure for the EF core

entity-framework-core

Question

I'm use EF core, and need advice, how correct create table. There is an article, it can be commented. It is also possible to comment on the comments themselves. How to describe the entities for the database?

So:

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;}
}

Or so:

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
2/23/2019 12:08:52 PM

Accepted Answer

Question is opinion-based and depends on your requirements. I would implement your logic with only two classes (Occam's razor) and maybe even without additional enumeration - you can just add a method with will check that one and only FK has a value - I guess that's the simplest approach.

0
2/23/2019 12:24:58 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow