Asp.net Core中多个模型与注释模型之间的关系

时间:2020-06-20 16:19:11

标签: asp.net entity-framework asp.net-core

我有3种型号:

  • 模型1:Article
  • 模型2:Course
  • 模型3:Episode

他们所有人都有很多评论。

如何在这些模型与Comment模型之间建立联系?

1 个答案:

答案 0 :(得分:3)

假设您的文章,课程和情节中的ID为long,在Comment模型中,您可以拥有类似的内容

public class Comment
{
    public long CommentId { get; set; }
    public string Message { get; set; }
    public DateTime EntryDate { get; set; }
    //Type to store Article, Course or Episode
    public string Type { get; set; }
    //TypeId to store the Id of the Article, Course or Episode
    public long TypeId { get; set; }
}

您也可以将public string Type { get; set; }更改为枚举,以使其更清晰易懂。