I'm using self-tracking entities with EF 4.0 and I see that there's no IsLoaded property for navigation objects which participate in a many to many relationship as there is on the standard EF objects. Therefore if you're querying on Person and don't Include Addresses then an empty list comes through for person.Addresses but there's no way to tell whether addresses have been loaded or the person just doesn't have any addresses.
Is there a way to tell whether a navigation property was loaded on self-tracking entities?
And if not is there a way to access the current ObjectQuery from the ObjectContext so that I can see what properti开发者_运维知识库es the user is trying to expand on and create custom IsLoaded properties?
Unfortunately my original plan to populate the IsLoaded properties on the self-tracking entities in the HandleObjectMaterialized method (called from the ObjectMaterialized event) didn't work as desired since the many to many collections are only populated after the event (see this post). And I wanted to iterate through the relationships in the context for each entity that it's tracking, test the IsLoaded property and set the corresponding IsLoaded property on my Self-tracking entity.
So instead I create extension methods for First() and ToList() called FirstWithLoaded() and ToListWithLoaded() to use reflection for this as:
public static T FirstOrDefaultWithLoaded<T>(this IQueryable<T> source) where T : new()
{
T result = default(T);
if (source != null)
{
//Call the base FirstOrDefault
result = source.FirstOrDefault();
var querySource = source as ObjectQuery<T>;
if (querySource != null)
{
PopulateIsLoaded(result, querySource.Context);
}
}
return result;
}
private static void PopulateIsLoaded(object inputEntity, ObjectContext dataContext)
{
var entry = dataContext.ObjectStateManager.GetObjectStateEntry(inputEntity);
//var relationShipManagerProperty = entryType.GetProperty("RelationshipManager");//.GetValue(entityType, null);
var relationShipManager = GetPropertyValue(entry, "RelationshipManager");// relationShipManagerProperty.GetValue(entry, null);
if (relationShipManager != null)
{
//get the relationships (this is a sealed property)
var relationships = GetPropertyValue(relationShipManager, "Relationships") as IEnumerable<RelatedEnd>;
if (relationships != null)
{
foreach (RelatedEnd relationship in relationships)
{
//check to see whether the relationship is loaded
var isLoaded = GetRelatedEndPropertyValue(relationship, "IsLoaded");
if (isLoaded != null && (bool)isLoaded)
{
//if the relationship is loaded then set the
//<NavigationPropertyName>IsLoaded on entry to true
var navigationProperty = GetRelatedEndPropertyValue(relationship, "NavigationProperty");
var identity = GetPropertyValue(navigationProperty, "Identity");
//get the IsLoaded property on entry
var isLoadedProperty = entry.Entity.GetType().GetProperty(identity + "IsLoaded");
if (isLoadedProperty != null)
{
isLoadedProperty.SetValue(entry.Entity, true, null);
}
}
}
}
}
}
private static object GetPropertyValue(object inputObject, string propertyName)
{
object result = null;
if (inputObject != null)
{
var property = inputObject.GetType().GetProperty(propertyName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
if (property != null)
{
result = property.GetValue(inputObject, null);
}
}
return result;
}
private static object GetRelatedEndPropertyValue(RelatedEnd inputObject, string propertyName)
{
object result = null;
if (inputObject != null)
{
PropertyInfo property = null;
property = inputObject.GetType().GetProperty(propertyName, BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
if (property != null)
{
result = property.GetValue(inputObject, null);
}
}
return result;
}
This solution is slightly dissapointing in that I had to access the sealed property "NavigationProperty" and then NavigationProperty.Identity in order to get the correct navigation (eg Person.Addresses instead of Person.Address). Hopefully something more elegant will present itself in the future.
Note in order for this to work I updated my Types T4 template to create the IsLoaded properties for me eg on Person I created an AddressesIsLoaded property for Addresses as:
<#
if (navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
{
#>
//The IsLoaded property for use on the client side when including collections
[DataMember]
<#=Accessibility.ForReadOnlyProperty(navProperty)#> bool <#=code.Escape(navProperty)#>IsLoaded
{
get; set;
}
<#
}
#>
There is no more IsLoaded in EF4. All default code is generated with implicit lazy loading. No need to call .Load() anymore.
Regardless the current SelfTrackingEntities t4 does not support lazy loading of any kind. You have to eager load navigation properties if you want to access them. Lazy loading is possible, your just going to have to edit the t4 template manually.
精彩评论