How can we turn off lazy loading?


The opposite of lazy loading is eager loading. In eager loading we load the objects beforehand. So the first thing is we need to disable lazy loading by setting “LazyLoadingEnabled” to false.

context.ContextOptions.LazyLoadingEnabled = false;

So now we have to explicitly tell EF what objects we want to load by using the “include” function. Below is a simple sample code where we tell EF to load customer as well as address objects by using the “include” function.
So now the customer object and the related address objects will be loaded in one query rather than multiple queries.

var employees = context.Customers.Include("Addresses").Take(5);