给定以下类结构
public class Parent
{
public Guid Id { get;
public List<BaseChild> Children { get; set; }
}
public abstract class BaseChild
{
public int Id { get; set; }
public string ChildName { get; set; }
}
public class NormalChild : BaseChild
{
public DateTime BirthDate { get; set; }
}
public class RichChild : BaseChild
{
public List<OffshoreAccount> OffshoreAccounts { get; set; }
}
public class OffshoreAccount
{
public string AccountNumber { get; set; }
public AccountInfo AccountInfo { get; set; }
}
查询父母数据以包含有关子女离岸账户的信息的最佳方式是什么?我想出了下面的解决方案,使用 ef-core 的显式加载,但感觉不对。有没有更优雅的解决方案?
var parent = Context.Set<Parent>()
.Where(o => o.Id == Guid.Parse(parentId))
.Include(o => o.Children)
.SingleOrDefault();
foreach (var child in parent.Children.OfType<RichChild>())
{
Context.Entry<RichChild>(child).Collection(f => f.OffshoreAccounts).Load();
foreach (var account in child.OffshoreAccounts)
{
Context.Entry<OffshoreAccount>(account).Reference(f => f.AccountInfo).Load();
}
}