Update (EF Core 2.1+):
Starting with v2.1, EF Core native supports Include on derived types through C# cast or as
operators.
e.g
.Include(e => e.Children)
.ThenInclude(e => ((RichChild)e).OffshoreAccounts)
.ThenInclude(e => e.AccountInfo)
or
.Include(e => e.Children)
.ThenInclude(e => (e as RichChild).OffshoreAccounts)
.ThenInclude(e => e.AccountInfo)
The documentation claims that the string
overload of Include
coudld also be used, e.g. according to it
.Include(e => "Children.OffshoreAccounts.AccountInfo")
should also work, but it doesn't (checked up to v3.1.4).
Original:
Currently there is no way to accomplish that in the parent query, but the explicit loading can be improved by using a combination of Entry
, Collection
, Query
, Include
/ ThenInclude
and Load
calls:
var parent = Context.Set<Parent>()
.Where(o => o.Id == Guid.Parse(parentId))
.Include(o => o.Children)
.SingleOrDefault();
Context.Entry(parent).Collection(e => e.Children)
.Query().OfType<RichChild>()
.Include(e => e.OffshoreAccounts)
.ThenInclude(e => e.AccountInfo)
.Load();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…