【问题标题】:EF Core - Discussion board with nested commentsEF Core - 带有嵌套评论的讨论板
【发布时间】:2021-02-25 21:52:03
【问题描述】:

我正在尝试创建一个类似于 Reddit 的讨论板,其中一个帖子可以有多个评论,每个评论可以有多个评论,每个评论可以有多个评论,等等。

我将如何编写查询以返回包含所有评论、所有评论的 cmets 以及所有评论的 cmets 等的特定帖子?我想过使用 .ThenInclude() 但这是不可能的,因为我不知道有多少嵌套的 cmets 提前。

这是我目前拥有的,但它只检索对帖子的直接回复,而不是嵌套的 cmets:

selected = await context.Posts
                    .Include(p => p.Author)
                    .Include(p => p.SavedBy)
                    .Include(p => p.HiddenBy)
                    .Include(p => p.UpvotedBy)
                    .Include(p => p.DownvotedBy)
                    .Include(p => p.Replies.Where(c => !c.HiddenBy.Contains(user)))
                        .ThenInclude(c => c.Author)
                    .Include(p => p.Replies)
                        .ThenInclude(c => c.SavedBy)
                    .Include(p => p.Replies)
                        .ThenInclude(c => c.HiddenBy)
                    .Include(p => p.Replies)
                        .ThenInclude(c => c.UpvotedBy)
                    .Include(p => p.Replies)
                        .ThenInclude(c => c.DownvotedBy)
                    .SingleAsync(p => p.Id == id);

型号:

public abstract class Entry
    {
        public Entry()
        {
            Replies = new List<Comment>();
            SavedBy = new List<ApplicationUser>();
            HiddenBy = new List<ApplicationUser>();
            UpvotedBy = new List<ApplicationUser>();
            DownvotedBy = new List<ApplicationUser>();
        }

        public int Id { get; set; }
        public string Content { get; set; }
        public DateTime DateCreated { get; set; }
        public int Upvotes { get; set; }
        public int Downvotes { get; set; }
        public int VoteScore { get; set; }

        public ApplicationUser Author { get; set; }
        public ICollection<Comment> Replies { get; set; }
        public ICollection<ApplicationUser> SavedBy { get; set; }
        public ICollection<ApplicationUser> HiddenBy { get; set; }
        public ICollection<ApplicationUser> UpvotedBy { get; set; }
        public ICollection<ApplicationUser> DownvotedBy { get; set; }
    }

    public class Post : Entry
    {
        public string Title { get; set; }
    }

    public class Comment : Entry
    {
        public Entry RepliedTo { get; set; }
        public Post Post { get; set; }
    }

数据库上下文:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
        {
        }

        public DbSet<Entry> Entries { get; set; }
        public DbSet<Post> Posts { get; set; }
        public DbSet<Comment> Comments { get; set; }
    }

【问题讨论】:

    标签: c# asp.net-mvc entity-framework .net-core entity-framework-core


    【解决方案1】:

    想一想查询在 SQL 而不是 C#/LINQ 中的外观。由于关系将是递归的,因此您需要为需要预取的许多层显式定义连接。这会让您丢失任何层之外的数据。或者,您可以使用由存储过程绑定的游标。不。相反,评论可以有一个指向其父评论的指针,但也可以有一个指向 MOST 父实体(原始帖子)的外键。然后,当您获取帖子的所有 cmets 时,可以将所有 cmets 作为平面数组检索,然后在 c# 中构建树(在 SQL 之外)。这应该会更高效。

    【讨论】:

      猜你喜欢
      • 2021-09-09
      • 2012-09-17
      • 2022-01-19
      • 2014-08-28
      • 1970-01-01
      • 2018-12-23
      • 2015-09-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多