Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Core Collection Load .. of a Collection

Using EF Core 1.1.0

I have a model that has collections that themselves have collections.

public class A {  
  public string Ay {get;set;}    
  public List<B> Bees {get;set;}
}

public class B {
  public string Be {get;set;}
  public List<C> Seas {get;set;}
}

public class C {
  public string See {get;set;}
  public bool InDark {get;set;}
  public List<D> Sigh {get;set;}
}

Now.. A is huge, and 99% of the time I don't care about B, so it doesn't get loaded. If I did load it, then it would be something like:

context.A.Include(a=>a.Bees).ThenInclude(b=>b.Seas).ThenInclude(c=>c.Sigh)...

Now let's say I already have A loaded up and the 1% happens for me to care about B. We don't have lazy loading yet, but the last release did give us explicit loading. Awesome.

context.Entry(A).Collection(a=>a.Bees).Load();

Problem seems to be that there isn't a way to include the additional collections inside B? Do I have no choice but to reload A with the .Include.ThenInclude.ThenInclude.Etc?

like image 985
imukai Avatar asked Jan 08 '17 03:01

imukai


1 Answers

Fortunately you have an option. Instead of directly calling Load, you can use Query method and apply as many Include / ThenInclude as you wish.

For your sample, it would be something like this:

context.Entry(A).Collection(a => a.Bees)
    .Query()
    .Include(b => b.Seas).ThenInclude(c => c.Sigh)...
    .Load();
like image 140
Ivan Stoev Avatar answered Oct 29 '22 23:10

Ivan Stoev