Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lazy loading ignoring Explicit Loading

I have a entity loaded with explicit loading but when i try access the loaded references it is loaded again with lazy loading without where clausule!

The only way i found is disable Lazy Loading which i can't!

I don't understand why it's loading again if i already explicit loaded the references.

Here's a example (i shorted a little bit for demonstration purposes):

        var employee = dbo.Employees
            .Where(m => m.StoreId == SessionContext.Store
                && m.Id == 10)
            .Include(m => m.Person)
            .FirstOrDefault();

        if (employee == null)
        {
            return HttpNotFound();
        }

        dbo.Entry(employee)
            .Collection(m => m.Stocks)
            .Query()
            .Where(m => ...)
            .Load();

        // LAZY LOADING HERE
        foreach (var stock in employee.Stocks)
        {
        }
like image 259
Trxplz0 Avatar asked Feb 13 '26 00:02

Trxplz0


1 Answers

You have to disable Lazy Load, EF will try to fetch all stocks when you iterate them

Wrap your context with a Using block

using(DbContext dbo=new DbContext())
{
   //Disable lazy loading
   dbo.Configuration.LazyLoadingEnabled=false;
   var employee = GetEmployee()...


   dbo.Entry(employee)
        .Collection(m => m.Stocks)
        .Query()
        .Where(m => ...)
        .Load();

}//Kill the context

    // NO LAZY LOADING HERE
    foreach (var stock in employee.Stocks)//If you don't disable Lazy Loading, EF will try to fetch all stocks
    {
    }

When using the Query method it is usually best to turn off lazy loading for the navigation property. This is because otherwise the entire collection may get loaded automatically by the lazy loading mechanism either before or after the filtered query has been executed.

https://msdn.microsoft.com/en-us/data/jj574232.aspx

Otherwise, if you don't want to disable Lazy Loading, you'll have to do what jbl says

var employeeStocksFiltered = dbo.Entry(employee) .Collection(m => m.Stocks) .Query() .Where(m => ...) .ToList(); 
foreach (var stock in employeeStocksFiltered ) { }
like image 149
The One Avatar answered Feb 14 '26 22:02

The One



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!