Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework: How to disable lazy loading for specific query?

Is there any way to disable lazy loading for specific query on Entity Framework 6? I want to use it regularly, but sometimes I want to disable it. I'm using virtual properties to lazy load them.

like image 662
Marco Alves Avatar asked Jun 03 '14 19:06

Marco Alves


People also ask

Can you disable lazy deferred loading?

You can turn off deferred loading by setting DeferredLoadingEnabled to false .

How does DB Contact disable lazy loading?

Lazy loading can be turned off for all entities in the context by setting a flag on the Configuration property to false as shown in the following example. After turning off lazy loading, now when you run the above example again you will see that the Enrollments are not loaded and only student data is retrieved.

How can you disable lazy loading from DbContext constructor?

You can turn off the lazy loading of all entities by making LazyLoadingEnabled property to false in the constructor of the DbContext class as shown below.


1 Answers

set the following code before the query you want to execute

context.Configuration.LazyLoadingEnabled = false; 
like image 167
Karthik Ganesan Avatar answered Sep 29 '22 06:09

Karthik Ganesan