Search code examples
entity-framework-6lazy-loadingdynamic-proxy

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

Solution

  • 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.