Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check Lazy Load property has loaded in EF6

I'm using class properties by reflection in some operations so when using DynamicProxy instance it causes to load entire DB. (700+ classes are related with each other).

Is it possible to check if lazy load property loaded or not? Disabling dynamic proxy generation (ProxyCreationEnabled = false) is not usable in my case.

Customer oCustomer = context.get(1);

if(oCustomer.Location.HasLoaded)
   do smt..

public class Customer
{
    public decimal? Id {get; set;}
    public virtual CustomerLocation Location{get; set;}
}

public class CustomerLocation
{
    public decimal? Id {get; set;}
    public string Detail {get; set;}
}
like image 634
hkutluay Avatar asked Jun 06 '16 06:06

hkutluay


People also ask

How do I turn off lazy loading in Entity Framework 6?

To turn off lazy loading for all entities in the context, set its configuration property to false.

What is lazy loading query?

What is Lazy Loading? Lazy Loading means the related entities are not loaded, until we iterate through them or bind them the data. By default, LINQ to SQL loads the related entities, using Lazy Loading. There is one-to-many relationship between Department and Employees entities.


1 Answers

Looks like you are seeking for DbReferenceEntry<TEntity, TProperty>.IsLoaded or DbReferenceEntry.IsLoaded property:

if (context.Entry(oCustomer).Reference(e => e.Location).IsLoaded)

or

if (context.Entry(oCustomer).Reference("Location").IsLoaded)

For collection type navigation properties, just use .Collection instead of .Reference.

like image 161
Ivan Stoev Avatar answered Sep 20 '22 19:09

Ivan Stoev