1. Back To Blog

How can we turn off lazy loading?(Entity framework interview questions with answers)

In case you are not aware of "lazy loading" , please first read this entity framework interview question "What is lazy loading in entity framework ?"


Below is a great ADO.NET Entity framework interview question video created by www.questpond.com which shows how to do CRUD using Entity framework.



The opposite of lazy loading 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);

 

Thanks questpond.com to provide this ADO.NET Entity framework interview question.

Shiv Prasad Koirala

Visit us @ www.questpond.com or call us at 022-66752917... read more

We are on Social