Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 523529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:28:19+00:00 2026-05-13T08:28:19+00:00

I’m using self-tracking entities with EF 4.0 and I see that there’s no IsLoaded

  • 0

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 properties the user is trying to expand on and create custom IsLoaded properties?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:28:19+00:00Added an answer on May 13, 2026 at 8:28 am

    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;
        }
    <#
            }
    #>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.