EF Core Included in several sublayer collections

Consider this cumulative root ...

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

... which I used so ...

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

How can I download all collections with a contact?

I tried this ...

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

I also tried this ...

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

In a related note, is it possible to express what should be returned as part of the total root using the free configuration?

+4
source share
1 answer

The template ThenIncludeallows you to specify a single path from the root to the sheet , therefore, to indicate the path to another sheet, you need to restart the process from the root to using the method Includeand repeat this for each sheet.

:

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

Source: https://habr.com/ru/post/1658481/


All Articles