EF Core-如何为已经存在一对多关系的同一实体设置多对多 [英] EF Core - How to setup many-to-many for same entity that already has a one-to-many relationship

查看:524
本文介绍了EF Core-如何为已经存在一对多关系的同一实体设置多对多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在帖子评论之间存在一对多的关系。一个帖子可以包含多个评论。我想做的是为评论创建多对多关系。我在 User Comment 之间也存在一对多的关系。

I currently have a one-to-many relationship between a Post and Comment. One Post can contain multiple Comments. What I want to do is create a many-to-many relationship for Comments. I also have a one-to-many relationship between User and Comment.

即每个注释应该能够包含注释的集合。例如。一个用户可以评论另一个用户的评论。我想保留注释的顺序,以便可以正确的顺序显示它们。

I.e. each Comment should be able to contain a collection of Comment. E.g. a user can comment on another user's comment. I want to preserve the ordering of comments so that I can display them in the correct order.

public class Comment
    {
        [Key]
        public Guid Id { get; set; }

        [Required, MaxLength(1000)]
        public string Message { get; set; }

        [Required]
        public DateTime Created { get; set; }

        //need to set this up
        public ICollection<Comment> ChildComments { get; set; }

        [Required]
        public Guid PostId { get; set; }

        [Required]
        public Post CommentForPost { get; set; }

        [Required]
        public Guid UserId { get; set; }

        [Required]
        public User CreatedBy { get; set; }
    }

在维持这种关系的同时,建立这种关系的正确方法是什么?我与其他实体之间的多对多关系?我最终会创建联接表吗?我不太确定如何建立我的EF关系以实现上述情况。

What is the correct way of setting up this relationship while maintaining the one-to-many relationships I have with other entities?? Do I end up creating a join table? I'm not quite sure how I should be setting up my EF relationship to achieve above scenario.

推荐答案


每个评论应该能够包含评论的集合

each Comment should be able to contain a collection of Comment



public class Comment
{
 [Key]
        public Guid Id { get; set; }

        [Required, MaxLength(1000)]
        public string Message { get; set; }

        [Required]
        public DateTime Created { get; set; }

        //need to set this up
        public ICollection<Comment> ChildComments { get; set; }

        [Required]
        public Guid PostId { get; set; }

        [Required]
        public Post CommentForPost { get; set; }

        [Required]
        public Guid UserId { get; set; }

        [Required]
        public User CreatedBy { get; set; }

   //this comment can be a child comment for (if is set) 
   // ParentCommentId is optional
           public string ParentCommentId {get;set;}
           public Comment ParentComment {get;set;}    
   //this comment can have many comments
           public ICollection<Comment> ChildComments {get;set;}

}

然后进行配置。您可以这样做:

then for config. you can do:

 builder.Entity<Comment>()
            .HasOne(x => x.ParentComment)
            .WithMany(x => x.ChildComments)
            .HasForeignKey(x => x.ParentCommentId);

这篇关于EF Core-如何为已经存在一对多关系的同一实体设置多对多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆