Is disconnecting Lazy Loading dangerous?

I have an ASP.NET MVC application using the Entity Framework for a data layer.

In one of my methods, I get data on the seasonal availability of the product, and then the best tax rate for the product.

public ProductList FetchProductSearchList(ProductSearchCriteria criteria)
{
    ...
    var avail = ProductAvailabilityTemplate.Get(criteria.ProductID);
    ...
    var tr = TaxRate.BestMatchFor(criteria.ProductID, criteria.TaxCode);
    ...
}

In the data layer for ProductAvailabilityTemplate.Get, I optimized the performance of my LINQ code. In particular, I installed ctx.ObjectContext.ContextOptions.LazyLoadingEnabled = false;to prevent the loading of some objects (via navigation properties) that I do not need in this scenario.

However, as soon as this change was made, I noticed that my TaxRates were not fully loaded because it ctx.ObjectContext.ContextOptions.LazyLoadingEnabledwas still false in my tax data level code. This meant that the object associated with TaxRate through the navigation property was not loaded.

, ctx.ObjectContext.ContextOptions.LazyLoadingEnabled = true; , , ​​. , , , . , , , .

.

+4
2

, EF, , , , .

, Includes, , . , , includeDetails:

public IEnumerable<Customer> LoadCustomersStartingWithName(string name, bool includeDetails)
{
    using (var db = new MyContext())
    {
        var customers = db.Customers;
        if (includeDetails)
            customers = customers.Include(x => x.Orders).Include(x => x.ContactPersons);

        customers = customers.Where(x => x.Name.StartsWith(name));

        return customers;        
    }
}

, EF6,

using System.Data.Entity;

+2

:

Lazy Loading

  • .
  • .
  • POCO (, virtual -)
  • , DbContext , .

  • ,
  • - .
  • DbContexts

FWIW, Lazy Loading, , , Lazy Loading Include d . . Lazy , (, ), .

, - , ORM. Lazy Loading, , Includes

+2

Source: https://habr.com/ru/post/1568325/


All Articles