1

I am currently using Lazy Proxies with ef-Core. For the most of my queries I really like and need the lazy loading, but I have one particular query which has a lot of dependencies to other entities.

Which looks something like this:

- Model
    - ICollection<Model2>
        - ICollection<Model3>
            - Model4
                - Model5
                - Model6

For this query it would be better to just load everything at once, it is a performance improvement of about 200%.

My question now, is there any way to just straight up load all related data?

What I am currently doing is something like this:

var entity1 = await DbContext.Entites1.FirstOrDefaultAsync(x => x.Id == 1);

var entities2 = await DbContext.Entry(entity1).Collection(x => x.Entites2).Query().ToListAsync();
foreach (var entity2 in entities2)
{
    var entities3 = await DbContext.Entry(entity2).Collection(x => x.Entites3).Query().Include(x => x.Entity4).ThenInclude(x => x.Entity5).Include(x => x.Entity4).ThenInclude(x => x.Entity6).ToListAsync();

    item.LessonEntries = entities3;
}

This is already way better, but it still does multiple queries.

If this is not possible, could this be achieved with the Include and ThenInclude methods to load all related data with only one query?

Twenty
  • 3,416
  • 2
  • 17
  • 47

1 Answers1

1

So actually, this question got asked several times already and somehow I didn't notice. Therefor I'll link the correct answer here.

You can enable and disable it by setting the LazyLoadingEnabled property on the ChangeTracker property of your DBContext instance.

Twenty
  • 3,416
  • 2
  • 17
  • 47