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 ..)
IsLoadedis property available inRelatedEndclass which is base forEntityReferenceandEntityCollection<...>.EntityObjectbased 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 toEntityCollection<...>(you still have to turn off lazy loading prior to that operation) but I’m not sure how to get easilyEntityReferenceexcept digging intoObjectStateEntryandRelationshipManager.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:
You can call this prior to your checks and lazy loading will not be executed. After your check you can again enable lazy loading.