Given the following 3 (simplified) classes:
When I am trying to load the JunctionClass
list...
var junctionClassList = _appDbContext.JunctionClass
.Include(jc => jc.ClassA)
.Include(jc => jc.ClassB).ToList();
the code doesn't load the ClassA
and ClassB
objects.
I have found a fix, but I don't think that I'm using EF Core the way it should be used:
var classAList = _appDbContext.ClassA.ToList();
var classBList = _appDbContext.ClassB.ToList();
var junctionClassList = _appDbContext.JunctionClass
.Include(jc => jc.ClassA)
.Include(jc => jc.ClassB).ToList();
This way, the junctionClassList
object will also load the classA
and classB
dependencies.
My question is this - is this the correct way to use lazy/eager loading? Or is there another way?
You are using Eager loading in this case using Include
. Lazy loading is introduced in EF Core 2.1. You need to enable it explicitly.