EF Core包含多個子級別集合

c# entity-framework entity-framework-core

考慮這個聚合根...

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

......我這樣用過......

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

如何通過聯繫人急切加載所有集合?

我試過這個......

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

我也試過這個......

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

在某種程度上相關的說明,是否可以使用流暢的配置表達應該作為聚合根的一部分返回的內容?

一般承認的答案

ThenInclude模式允許您指定從根到單個葉子的路徑,因此,為了指定另一個葉子的路徑,您需要使用Include方法從根重新啟動該過程,並為每個葉子重複該過程。

對於您的樣本,它將是這樣的:

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


Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow