嵌套 tph 继承成员的 ef-core 负载集合属性 [英] ef-core load collection property of nested tph inherited member

查看:11
本文介绍了嵌套 tph 继承成员的 ef-core 负载集合属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定以下类结构

 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 的显式加载,但感觉不对.有没有更优雅的解决方案?

What is the best way to query parent data to include information about the children's offshore accounts?. I came up with the solution below, using ef-core's explicit loading, but it just doesn't feel right. Is there a more elegant solution?

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();
            }
     }

推荐答案

更新(EF Core 2.1+):

从 v2.1 开始,EF Core 原生支持 通过 C# 强制转换或 as 运算符包含在派生类型上.

Starting with v2.1, EF Core native supports Include on derived types through C# cast or as operators.

例如

.Include(e => e.Children)
    .ThenInclude(e => ((RichChild)e).OffshoreAccounts)
        .ThenInclude(e => e.AccountInfo)

.Include(e => e.Children)
    .ThenInclude(e => (e as RichChild).OffshoreAccounts)
        .ThenInclude(e => e.AccountInfo)

文档声称也可以使用 Includestring 重载,例如根据它

The documentation claims that the string overload of Include coudld also be used, e.g. according to it

.Include(e => "Children.OffshoreAccounts.AccountInfo")

应该也可以,但不行(检查到 v3.1.4).

should also work, but it doesn't (checked up to v3.1.4).

原文:

目前无法在父查询中实现,但可以通过使用EntryCollectionQuery的组合来改进显式加载Include/ThenIncludeLoad 调用:

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();

这篇关于嵌套 tph 继承成员的 ef-core 负载集合属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆