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

C#Entity FrameworkLazy Loading

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

set the following code before the query you want to execute

context.Configuration.LazyLoadingEnabled = false;

Solution 2 - C#

You can disable Lazy loading for a specific query as follows :

public static Cursos GetDatosCursoById(int cursoId)
{
    using (var bd = new AcademyEntities())
    {
        try
        {
            bd.Configuration.ProxyCreationEnabled = false;
            return bd.Cursos.FirstOrDefault(c => c.cursoId == cursoId);
        }
        catch (Exception ex)
        {
            return null;
        }
    }
}

Solution 3 - C#

I might be missing something here, but rather than changing the configuration each time, might another approach be to use .Include() on only those queries where you want to eager load?

Suppose we have a Product class which has a navigation property to a Colour class, you might load the Colour for a Product like this -

var product = _context.Products
	.Where(p => p.Name == "Thingy")
        .Include(x => x.Colours)
        .ToList();

Solution 4 - C#

In EF Core: context.ChangeTracker.LazyLoadingEnabled = false;

Per this answer.

Solution 5 - C#

Go to your diagram properties and find a property designated to lazy loading and disable it.

If you are using code first then go to your config area and disable it from there with:

this.Configuration.LazyLoadingEnabled = false;

Solution 6 - C#

Another approcah for another EF Version (Entity Framework 5)

//Note: ContextOptions instead of ChangeTracker or Configuration
context.ContextOptions.LazyLoadingEnabled = false; 

Solution 7 - C#

Suppose you have this:

IOrderedQueryable<Private.Database.DailyItem> items;
using (var context = new Private.Database.PrivateDb())
{
    context.Configuration.LazyLoadingEnabled = false;
    items = context.DailyItem.OrderBy(c => c.sortOrder).OrderByDescending(c => c.isFavorite);
}

You'd still get lazy loading, despite the explicit setting of not to. The fix is easy, change it to this:

List<Private.Database.DailyItem> items;
using (var context = new Private.Database.PrivateDb())
{
    // context.Configuration.LazyLoadingEnabled = false;
    items = context.DailyItem.OrderBy(c => c.sortOrder).OrderByDescending(c => c.isFavorite).ToList();
}

Solution 8 - C#

> i just do this in every class that need disable lazy loading and in every class just call the db without lazyloading everything work fine

    private DataContext db;

    public TheClass ()
    {
        db = new DataContext(ConString);
        db.Configuration.LazyLoadingEnabled = false;
    } ​

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarco AlvesView Question on Stackoverflow
Solution 1 - C#Karthik GanesanView Answer on Stackoverflow
Solution 2 - C#William BallesterosView Answer on Stackoverflow
Solution 3 - C#ParrybirdView Answer on Stackoverflow
Solution 4 - C#Matt JenkinsView Answer on Stackoverflow
Solution 5 - C#JuanView Answer on Stackoverflow
Solution 6 - C#fuboView Answer on Stackoverflow
Solution 7 - C#StrongholdView Answer on Stackoverflow
Solution 8 - C#Martin MartonoView Answer on Stackoverflow