91

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.

Elrond_EGLDer
  • 47,430
  • 25
  • 189
  • 180
Marco Alves
  • 2,645
  • 3
  • 21
  • 33

7 Answers7

78

set the following code before the query you want to execute

context.Configuration.LazyLoadingEnabled = false;
Karthik Ganesan
  • 3,864
  • 1
  • 22
  • 37
41

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;
        }
    }
}
22

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();
Parrybird
  • 730
  • 13
  • 17
  • 1
    For me this is the best Answer here! – Ian Feb 21 '17 at 10:47
  • This falls short if you only want to eager load "products", without any includes. – Mackan Feb 01 '18 at 08:37
  • So you'd want to get 'Products' without any of their related objects, or 'Products with all their related objects?' – Parrybird Feb 01 '18 at 09:17
  • 1
    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. – Richard Petheram Jul 02 '18 at 07:18
  • 5
    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. – Doug Oct 15 '18 at 17:27
  • What will happen if I haven't turned off the lazyloading and use include? Can any one tell me how it will behave? – Sumesh Es Jul 24 '20 at 07:10
15

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;
ThiagoPXP
  • 4,952
  • 3
  • 29
  • 43
Juan
  • 1,282
  • 13
  • 19
  • 6
    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. – Juan Jan 24 '17 at 19:00
13

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

Per this answer.

Matt Jenkins
  • 2,507
  • 1
  • 24
  • 32
  • 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. – Gert Arnold Apr 02 '21 at 07:15
3

Another approcah for another EF Version (Entity Framework 5)

//Note: ContextOptions instead of ChangeTracker or Configuration
context.ContextOptions.LazyLoadingEnabled = false; 
fubo
  • 39,783
  • 16
  • 92
  • 127
2

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();
}
Stronghold
  • 118
  • 1
  • 6