1

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.

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

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();
GregoryHouseMD
  • 2,168
  • 1
  • 21
  • 37
  • Not my first choice, but as an alternative you may consider to use raw sql, views or stored procedures for difficult queries. https://stackoverflow.com/questions/35305825/raw-sql-queries-and-entity-framework-core –  Aug 29 '17 at 20:58
  • 1
    https://github.com/aspnet/EntityFrameworkCore/issues/3910 this would allow you to write Include/ThenInclude on derived types in 2.1 release – Smit Sep 12 '17 at 00:33

1 Answers1

0

I would advise against using Entity Framework Core if you have that option. 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.

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.

Maritim
  • 2,111
  • 4
  • 29
  • 59
  • I'd love to be able to use the full framework, but the project is suppose to be .NET Core only. This is pretty much the most complex query I'm writing so I can live with my current solution until they add Lazy Loading. – GregoryHouseMD Aug 29 '17 at 20:07
  • Out of curiosity, why the decision to make it Core only? – Maritim Aug 29 '17 at 20:09
  • Would love to explain it, but not my decision to make :) – GregoryHouseMD Aug 29 '17 at 20:11
  • 1
    Roger that! In that case I recommend keeping an eye on the roadmap. Sadly they've not even committed to make lazy loading happen this year, but they will try their very best :( – Maritim Aug 29 '17 at 20:12
  • 1
    I am following the roadmap (mostly because of lazy loading) but actually [this](https://github.com/aspnet/EntityFrameworkCore/issues/3910) is the feature I should be waiting for :) – GregoryHouseMD Aug 29 '17 at 20:34