简体   繁体   English

使用EF Core包含所有继承的类的所有属性

[英]Include all the properties from all the inherited classes using EF Core

When using EF6, I was using lazy loading so I never had this issue, but with EFCore, I don't know if this is possible with a single query. 使用EF6时,我使用的是延迟加载,所以我从来没有遇到过这个问题,但是使用EFCore,我不知道使用单个查询是否可以做到这一点。

I have the following class structure 我有以下课程结构

class A { public B b; }
class B { public ICollection<C> list_c; }
class C { public ICollection<D> list_d; }
abstract class D { public long c_id; }
class Da { public E e; }
class Db { public F f; }

I need a list of all the D objects, but with access to their e and f properties respectively. 我需要所有D对象的列表,但需要分别访问它们的ef属性。 I have a working query at the moment where I query _db.D over a list of c_id 's that I fetch using the first half of the query below, but with that approach, I send one query to get all the c_id 's and then one query per type (I have 4 types). 目前,我在使用以下查询的前半部分获取的c_id列表中查询_db.D ,有一个有效的查询,但是使用这种方法,我发送了一个查询以获取所有c_id和那么每种类型一个查询(我有4种类型)。

I was wondering if I can make it work with one call that looks something like this: 我想知道是否可以通过一个看起来像这样的呼叫使其工作:

_db.As.Include(x => x.b)
      .ThenInclude(x => x.list_c)
      .ThenInclude(x => x.list_d)
      // some magic here
      .FirstOrDefaultAsync(x=> x.Id = model.Id);

EDIT: 编辑:

At the moment this is how I make the list: 目前,这是我列出清单的方式:

var a = await _db.As.Include(x => x.b)
                    .ThenInclude(x => x.list_c)
                    .FirstOrDefaultAsync(x=> x.Id = model.Id);

var result = await _db.Ds.OfType<Da>()
                         .Include(x=>x.e)
                         .Where(x=>a.b.list_c.Any(y=>y.Id == x.c_id))
                         .Select(x=>(D)x)
                   .Concat(_db.Ds.OfType<Db>()
                         .Include(x=>x.f)
                         .Where(x=>a.b.list_c.Any(y=>y.Id == x.c_id))
                         .Select(x=>(D)x)).
                   .ToListAsync();

I would advise against using Entity Framework Core if you have that option. 如果可以的话,我建议不要使用Entity Framework Core。 Check the roadmap at https://github.com/aspnet/EntityFrameworkCore/wiki/Roadmap - It currently doesn't have lazy loading to name of its (very annoying) shortcomings. https://github.com/aspnet/EntityFrameworkCore/wiki/Roadmap上检查路线图-它目前没有延迟加载,这是(非常令人讨厌的)缺点的名称。

You can have your data access layer in a .NET 4.6 project and have your consuming project(s), even if the consuming is based on Core, reference that project without difficulty. 您可以在.NET 4.6项目中拥有数据访问层,并拥有使用中的项目,即使使用中基于Core的用户也可以毫不费力地引用该项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM