7

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;}
}
hkutluay
  • 6,794
  • 2
  • 33
  • 53

1 Answers1

10

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.

Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343
  • 1
    Thanks @Ivan Stoev, As a note for future, It's working when object state is not Detached. – hkutluay Jun 06 '16 at 10:05
  • Is there a way to check it without having the context (for example during validation of the entity)? – bubi Apr 07 '17 at 10:44
  • 1
    @bubi I wouldn't say absolutely impossible, but would be quite hard and has to dial with EF internals. – Ivan Stoev Apr 07 '17 at 11:22