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

92,281

Solution 1

set the following code before the query you want to execute

context.Configuration.LazyLoadingEnabled = false;

Solution 2

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

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

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

Per this answer.

Solution 5

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;
Share:
92,281
Marco Alves
Author by

Marco Alves

Updated on July 08, 2022

Comments

  • Marco Alves
    Marco Alves almost 2 years

    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.

  • Juan
    Juan over 7 years
    A lot of people are visiting this question and I want to say, people DONT WRITE QUERIES WITH OUT TAKING A LOOK TO THE EXECUTION PLAN. Always know what your code send to the database or you will have performance problems. You can use linq pad or other tools to view the real query and check.
  • Ian
    Ian about 7 years
    For me this is the best Answer here!
  • Mackan
    Mackan over 6 years
    This falls short if you only want to eager load "products", without any includes.
  • Parrybird
    Parrybird over 6 years
    So you'd want to get 'Products' without any of their related objects, or 'Products with all their related objects?'
  • Richard Petheram
    Richard Petheram almost 6 years
    Much more useful answer. This controls the specific sub-ordinate tables that are loaded at the point where the query is being constructed. For any real world problem this has to be the way to go.
  • Doug
    Doug over 5 years
    It is useful in a different way... if you do it this way one could still get lazy loading for another collection from 'Products'. Actually disabling lazy loading is more effective to guarantee that all data needed is fetched in advance and avoids creating hidden performance bottlenecks.
  • Sumesh Es
    Sumesh Es almost 4 years
    What will happen if I haven't turned off the lazyloading and use include? Can any one tell me how it will behave?
  • Gert Arnold
    Gert Arnold about 3 years
    It would be useful to mention here that lazy loading never occurs in EF core unless it's explicitly configured. Then it can be switched off in individual cases.