EF Core Include on multiple sub-level collections

前端 未结 1 1710
一向
一向 2020-12-11 17:25

Consider this aggregate root...

class Contact 
{
    ICollection Addresses { get; set; }
    ICollection Items { get         


        
相关标签:
1条回答
  • 2020-12-11 17:38

    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

    0 讨论(0)
提交回复
热议问题