0

When dynamically adding includes to the entity query i.e.

ObjectQuery<Address> oQuery = oAddressingEntitiesContext.Addresses.Include("StreetName");

if (sResultOption == "FULL")
{
    oQuery = oQuery.Include("AddressLocation").Include("AddressIdentifiers");
}

IQueryable<Address> oResult = oQuery.Where(oParser.getSearchPredicate());

Is there is a way to determine when looking at query results downstream if the entity has the included AddressLocation & AddressIdentifiers related entities by looking at the Address entity?

Ideally something like "IsLoaded" would be helpful

foreach (Address oAddress in oResult)
{
    if (oAddress.AddressLocation.IsLoaded)
    {
       ...
    }
}

It seems any references to the child related entities cause ef to try to load them, due to the lazy load. (I am receiving an error when accessing the related entity when it wasn't included that there is already an open datareader ..)

Martin Buberl
  • 45,844
  • 25
  • 100
  • 144
trouta
  • 426
  • 3
  • 12

1 Answers1

1

IsLoaded is property available in RelatedEnd class which is base for EntityReference and EntityCollection<...>. EntityObject based entities use these classes to expose information about navigation properties but POCOs don't. In case of proxied entities you can usually convert collection navigation property to EntityCollection<...> (you still have to turn off lazy loading prior to that operation) but I'm not sure how to get easily EntityReference except digging into ObjectStateEntry and RelationshipManager.

Anyway your biggest problem is probably the exception when you check the property. This can be solved by two options: either you allow lazy loading to be performed or you turned it off:

context.ContextOptions.LazyLoadingEnabled = false;

You can call this prior to your checks and lazy loading will not be executed. After your check you can again enable lazy loading.

Community
  • 1
  • 1
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670