Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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 <a href="http://blogs.msdn.com/adonet/" rel="nofollow noreferrer">this</a> 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.</p> <p>So instead I create extension methods for First() and ToList() called FirstWithLoaded() and ToListWithLoaded() to use reflection for this as:</p> <pre><code>public static T FirstOrDefaultWithLoaded&lt;T&gt;(this IQueryable&lt;T&gt; source) where T : new() { T result = default(T); if (source != null) { //Call the base FirstOrDefault result = source.FirstOrDefault(); var querySource = source as ObjectQuery&lt;T&gt;; 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&lt;RelatedEnd&gt;; if (relationships != null) { foreach (RelatedEnd relationship in relationships) { //check to see whether the relationship is loaded var isLoaded = GetRelatedEndPropertyValue(relationship, "IsLoaded"); if (isLoaded != null &amp;&amp; (bool)isLoaded) { //if the relationship is loaded then set the //&lt;NavigationPropertyName&gt;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; } </code></pre> <p>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.</p> <p>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:</p> <pre><code>&lt;# if (navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many) { #&gt; //The IsLoaded property for use on the client side when including collections [DataMember] &lt;#=Accessibility.ForReadOnlyProperty(navProperty)#&gt; bool &lt;#=code.Escape(navProperty)#&gt;IsLoaded { get; set; } &lt;# } #&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload