在EF Core中实施递归属性加载 [英] Implementing recursive property loading in EF Core

查看:378
本文介绍了在EF Core中实施递归属性加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是.NET Core 1.1.0,EF Core 1.1.0,VS 2015.

I'm on .NET Core 1.1.0, EF Core 1.1.0, VS 2015.

我正在编写一个用于发布/评论的系统,我需要一个函数来加载评论及其所有子级及其相关属性.这是我的课程的简化版本:

I'm writing a system for posts/comments, and I need a function to load a comment and all of its children and their associated properties. Here's a simplified version of my classes:

public class Comment
{
    public long Id { get; set; }

    public string Content { get; set; }

    public User User { get; set; }

    public ICollection<Comment> Replies { get; set; }
}

public class User
{
    public long Id { get; set; }

    public string Name { get; set; }

    public Avatar Avatar { get; set; }
}

public class Avatar
{
    public string Url { get; set; }
}

任何给定的评论都可以有任意数量的回复:

Any given comment can have any number of replies:

-PARENT
    -CHILD 1
    -CHILD 2
        -CHILD 3
    -CHILD 4
    -CHILD 5
        -CHILD 6
            -CHILD 7

因此,给定父注释的ID,我需要加载整个树,包括用户及其各自的头像. (我在其他地方有控件来确保这些树不会变得笨拙,在这一点上我不担心可能会捕获太多数据.)

So, given the ID of the parent comment, I need to load the entire tree, including users and their respective avatars. (I have controls elsewhere to make sure these trees don't become unwieldy, I'm not concerned at this point about potentially grabbing way too much data.)

EF中的加载相关数据页核心文档非常有帮助,但是我不确定如何最好地解决这个问题.我已经尝试过将一些东西放在一起,但是我无法概念化如何将它们放在一起.再次说明:我正在使用EF Core 1.1.0,因此可以访问显式加载"部分中的功能.

The Loading Related Data page in the EF Core docs is very helpful, but I'm not sure how to best handle this. I've experimented putting some things together but I just can't conceptualize how to fit it all together. To note again: I'm using EF Core 1.1.0, so I do have access to the functions in the "Explicit loading" section.

如何在给定父注释ID的情况下加载整个注释树?

推荐答案

我没有数据库,所以我只是在内存中使用它,但是如果您遵循我的意见,它将为您工作.请注意,我的内存中有对象,只有ID为2的评论才有回复.

I do not have a database so I just did it in memory but if you follow my comments, it will work for you. Notice the objects I have in memory, only comment with id 2 has replies.

LoadComment方法是所有事情发生的地方.剩下的就是我需要的设置代码.

LoadComment method is where everything happens. The rest is just setup code I needed.

class Program
{
    static void Main(string[] args)
    {
        var result = LoadComment(1, null);
        Console.ReadKey();

    }



public static Comment LoadComment(long id, Comment com) 
{
   Comment res = new Comment();
   if( com == null ) 
   {
      // You would call your context here and write db.Single(x => x.Id == id).Include(x => x.User.Avatar);
      var first = db.Single( x => x.Id == id );

      res = new Comment { Id = first.Id, Replies = first.Replies.ToList(), User = first.User };
      foreach( var item in first.Replies ) 
      {
         LoadComment( item.Id, item );
      }
   }
   else 
   {
      // You would call your context here and write db.Single(x => x.Id == id).Include(x => x.User.Avatar);
      var child = db.SingleOrDefault( x => x.Id == id );
      if( child == null ) 
      {
         return null;
      }
      com.Replies = new List<Comment>();
      com.Replies.Add( new Comment { Id = child.Id, Replies = child.Replies.ToList(), User = child.User } );
      foreach( var item in child.Replies ) 
      {
         LoadComment( item.Id, com );
      }
   }


   return res;
}

    private static Comment cm1 = new Comment
    {
        Id = 1,
        User = new User { Id = 1, Avatar = new Avatar { Url = "1" } },
        Replies = new List<Comment> {
        new Comment { Id = 2 },
        new Comment { Id = 3 },
        new Comment { Id = 4 },
        new Comment { Id = 5 } },
        Content = "ContentForCommentId1"
    };

    private static Comment cm2 = new Comment
    {
        Id = 2,
        User = new User { Id = 2, Avatar = new Avatar { Url = "2" } },
        Replies = new List<Comment> {
        new Comment { Id = 22 },
        new Comment { Id = 33 },
        new Comment { Id = 44 },
        new Comment { Id = 55 } },
        Content = "ContentForCommentId2"
    };
    private static List<Comment> db = new List<Comment> { cm1, cm2 };

}

这篇关于在EF Core中实施递归属性加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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