Disable Lazy Loading for a specific query

Asked

Viewed 256 times

1

I have a problem of slowness at the time of bringing the data.

I have in the table 16 thousand records that are taking about 6 seconds to be brought from the bank.

I noticed that when bringing these records this being brought also the related records due to the Lazy loading. However, in this query in particular there is no need to bring the related records.

I think I could buy some time if I don’t bring these unnecessary records, follow the code:

public IList<NotaMercadoria> GetAll(long pIdCadEmpresa)
{
    return entity.NotaMercadoria.Where(x => x.IDCadEmpresa == pIdCadEmpresa).ToList();
}

Since there is no need to bring the related records, is there any way to disable the Lazy loading for this query specifically?

1 answer

3


Just disable it before fetching the data.

public IList<NotaMercadoria> GetAll(long pIdCadEmpresa)
{
    entity.Configuration.LazyLoadingEnabled = 
    entity.Configuration.ProxyCreationEnabled = false;

    return entity.NotaMercadoria.Where(x => x.IDCadEmpresa == pIdCadEmpresa).ToList();
}
  • Thank you very much, it only makes me doubt, when Lazy Loading is disabled it brings the related table but with no data in it, right? I can post an image to make it easier if I find it necessary.

  • @Brayan That’s right. The produced select will be only on the main table without any Join.

  • Ah understood, thank you very much :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.