Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Core Include on multiple sub-level collections

Consider this aggregate root...

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?

I tried this...

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)

On a somewhat related note, is it possible to express what should be returned as part of an aggregate root using fluent configuration?

like image 438
Matthew Layton Avatar asked Oct 21 '16 16:10

Matthew Layton


People also ask

How to include multiple levels of related data in efef core?

EF Core has a new extension method ThenInclude (). You can drill down thru relationships to include multiple levels of related data using the ThenInclude method.

Is it possible to sort the contents of an EF collection?

This is possible for navigation "chains" that are all references, or when they end with a single collection. This feature was introduced in EF Core 5.0. When applying Include to load related data, you can add certain enumerable operations to the included collection navigation, which allows for filtering and sorting of the results.

What are the enumerable operations in EF Core?

This feature was introduced in EF Core 5.0. When applying Include to load related data, you can add certain enumerable operations to the included collection navigation, which allows for filtering and sorting of the results. Supported operations are: Where, OrderBy, OrderByDescending, ThenBy, ThenByDescending, Skip, and Take.

How to load multiple Navigations in EF Core?

You can also load multiple navigations using a single Include method. This is possible for navigation "chains" that are all references, or when they end with a single collection. This feature was introduced in EF Core 5.0.


1 Answers

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
    ...

Reference: Loading Related Data - Including multiple levels

like image 169
Ivan Stoev Avatar answered Oct 20 '22 12:10

Ivan Stoev