Entity Framework Core - lazy loading doesn't work

asp.net-core-2.0 c# entity-framework-core

Question

Given the following 3 (simplified) classes:

  • ClassA { int Id; }
  • ClassB { int Id; }
  • JunctionClass { ClassA classA; ClassB classB; }

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?

1
1
10/7/2018 1:52:35 PM

Popular Answer

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.

0
10/7/2018 2:42:16 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow