EF Core包含在多个子级别集合中 [英] EF Core Include on multiple sub-level collections

查看:288
本文介绍了EF Core包含在多个子级别集合中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑此汇总根...

class Contact 
{
    ICollection<ContactAddress> Addresses { get; set; }
    ICollection<ContactItem> Items { get; set; }
    ICollection<ContactEvent> Events { get; set; }
}

...我曾经这样使用...

...which I have used like so...

class Person 
{
    Contact ContactDetails { get; set; }
}

我如何渴望与联系人一起加载所有馆藏?

How do I eager load all of the collections with the contact?

我尝试过这个...

Context
    .Set<Person>()
    .Include(o => o.ContactDetails)
    .ThenInclude(o => o.Addresses)
    .ThenInclude(????)
    . ...

我也尝试过此方法...

I've also tried this...

Context
    .Set<Business>()
    .Include(o => o.ContactDetails.Addresses)
    .Include(o => o.ContactDetails.Events)
    .Include(o => o.ContactDetails.Items)

在一个有点相关的注释上,是否可以使用流利的配置表示应该作为聚合根的一部分返回的内容?

推荐答案

ThenInclude 模式可让您指定从根到叶的路径,因此,为了指定到另一个叶子的路径,您需要使用 Include 方法从根目录重新启动该过程,并对每个叶子重复该过程。

The ThenInclude pattern allows you to specify a path from the root to a single leaf, hence in order to specify a path to another leaf, you need to restart the process from the root by using the Include method and repeat that for each leaf.

对于您的示例,如下所示:

For your sample it would be like this:

Context.Set<Person>()
    .Include(o => o.ContactDetails).ThenInclude(o => o.Addresses) // ContactDetails.Addresses 
    .Include(o => o.ContactDetails).ThenInclude(o => o.Items) // ContactDetails.Items
    .Include(o => o.ContactDetails).ThenInclude(o => o.Events) // ContactDetails.Events
    ...

参考加载相关数据-包括多个级别

这篇关于EF Core包含在多个子级别集合中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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