Dot Net Code Tips
Toggle navigation
Home
Categories
Azure
C#
CSS / Less
Network
Powershell
Razor
Security
SQL
Text Files
Visual Studio
Web.config
About
Contact
Make Entity Frameworks read lazy load collections and objects
There are several methods to force the loading of lazy-load properties
Use Include() in the Linq query
Include() instructs Entity Frameworks to select the related object in the SQL query and is the fastest way to load related objects.
var
obj = context.SomeList.Where(a => a.Id == id).Include(b => b.AnotherObject).FirstOrDefault();
Use the context Entry() method to load a reference or a collection
Use context.Entry().Reference().Load() and context.Entry().Collection().Load();
context.Entry(a).Reference(b => b.AnotherObject).Load();
context.Entry(a).Collection(c => c.AnotherCollection).Load();