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 1037649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:52:36+00:00 2026-05-16T14:52:36+00:00

I am attempting to perform the LoadProperty operation from my context to load a

  • 0

I am attempting to perform the LoadProperty operation from my context to load a navigation property of a navigation property.

My setup is that I have an EntityA, which contains a list of EntityB’s, and each EntityB contains a list of EntityC’s. I am doing the following programatically:

public virtual List<T> LoadProperty(List<T> entities, string property)
{
    using (MyContext context = new MyContext())
        foreach (T entity in entities)
        {
            context.AttachTo(typeof(T).Name, entity);
            context.LoadProperty(entity, property);
        }

    return entities;
}

I call it as such:

LoadProperty(entityA, "EntityB.EntityC");

I know that the NavigationProperty path is correct, however, this is not working. Is there a way to get this to load?

Edit:
Working example using Includes:

using (MyContext context = new MyContext())
{
    var query = from entityA in context.EntityA.Include("EntityB").Include("EntityB.EntityC")
                where entityA.Id == id
                select entityA;

    return query.ToList();
}
  • 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-16T14:52:37+00:00Added an answer on May 16, 2026 at 2:52 pm

    First off, your method call context.AttachTo(typeof(T).Name, entity) is not correct and you’ll get an InvalidOperationException. ObjectContext.AttachTo Method shows:

    public void AttachTo(string entitySetName, Object entity)
    

    So we need to pass the EntitySet name and not the Entity name itself. But the good news is we can get the EntitySet name from MetadataWorkspace by having the Entity name. The code below shows how.

    Now if you have 3 levels of object composition and EntityB and EntityC are navigation properties of type EntityCollections, then I don’t think you could load them both with one call to LoadProperty, but you can do it by call LoadProperty twice, here is how it can be done:

    using System.Data.Metadata.Edm;
    
    public virtual List<T> LoadProperty(List<T> entities, string property) {
        using (TrialsContext context = new TrialsContext()) {
    
            EntityContainer container = context.MetadataWorkspace
                                               .GetEntityContainer(context.DefaultContainerName,
                                                                   DataSpace.CSpace);
            EntitySetBase entitySet = container.BaseEntitySets
                                               .Where(item => 
                                                      item.ElementType.Name.Equals(typeof(T).Name))
                                               .FirstOrDefault();
    
            foreach (T entity in entities) {
                context.AttachTo(entitySet.Name, entity);
                context.LoadProperty(entity, property);
            }
    
        return entities;
    }
    

    And you will call it:

    
    // To load EntityA Nav property:
    LoadProperty(entityB, "EntityA");
    
    // To Load EntityC Nav property: 
    //Let's assume the nav property name for EntityC on EntityB is EntityCList
    LoadProperty(entityB, "EntityCList");
    
    

    This way you will have the full object graph constructed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to perform a Storyboard segue from a UIbutton that is a
I am attempting to perform integration testing on services that I have developed. Part
I'm attempting to perform the most basic navigation with JQuery and I'm failing miserably.
I'm attempting to perform a DML operation against an Entity Framework model, specifically, an
Attempting to build a C# NPAPI plugin I have found a tutorial which describes
I am attempting to perform an inner join on two tables so that I
I'm attempting to perform dynamic sorting of data that I'm putting into grids into
I have a web page that connects to a SQL Server 2008 instance, attempting
I'm attempting to perform a full outer join on two tables that are not
I am attempting to perform the following query: SELECT SUM(cash) AS total_cash FROM users

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.