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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:11:42+00:00 2026-06-13T17:11:42+00:00

I often use the System.Data.Entity.DbExtensions Include() method to cause complex entity fields to be

  • 0

I often use the System.Data.Entity.DbExtensions Include() method to cause complex entity fields to be included in query results from my repositories. However, when I project my entities into new classes, I seem to lose this “concretization” of included complex entity fields. For example, say I wanted to return an Event object from my repo, and be able to access the complex entity field Assessment:

public class EventRepository {
...
    public IList<Event> GetEvents() {
        using (var context = new MyDatabaseContext()) {
            return context.Events
                .Include(evnt => evnt.ActualAssessment)
                .ToList();
        }
    }
...
}

I can then run the following code without a hitch because of the Include I used above:

var repoEvents = new EventRepository();
var events = repoEvents.GetEvents();
Console.WriteLine(events[0].ActualAssessment.AssessmentDate.ToString());

But say I now want to project the Events into a wrapper object ExtendedEvent with some extra info, like this:

public class EventRepository {
...
    public IList<ExtendedEvent> GetExtendedEvents() {
        using (var context = new MyDatabaseContext()) {
            return context.Events
                .Include(evnt => evnt.ActualAssessment)
                .Select(evnt => new {
                    TheEvent = evnt,
                    SomeExtraData = 123
                })
                .ToList()
                .Select(evntInfo => {
                    return new ExtendedEvent {
                        TheEvent = evntInfo.TheEvent,
                        SomeExtraData = evntInfo.SomeExtraData
                    };
                })
                .ToList();
        }
    }
...
}

I now try and run this code:

var repoEvents = new EventRepository();
var extendedEvents = repoEvents.GetExtendedEvents();
Console.WriteLine(extendedEvents[0].TheEvent.ActualAssessment.AssessmentDate.ToString());

This gives me the error “The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.” – so ActualAssessment has not been eager-loaded despite my use of Include, apparently because I projected it into a new wrapper object. How can I cause ActualAssessment to be included?

  • 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-06-13T17:11:43+00:00Added an answer on June 13, 2026 at 5:11 pm

    Yes, Include is ignored in projections. What you can try is to make the related navigation property part of your projection into the anonymous object:

    public IList<ExtendedEvent> GetExtendedEvents() {
        using (var context = new MyDatabaseContext()) {
            return context.Events
                .Select(evnt => new {
                    TheEvent = evnt,
                    SomeExtraData = 123,
                    ActualAssessment = evnt.ActualAssessment
                })
                .ToList()
                .Select(evntInfo => {
                    return new ExtendedEvent {
                        TheEvent = evntInfo.TheEvent,
                        SomeExtraData = evntInfo.SomeExtraData
                    };
                })
                .ToList();
        }
    }
    

    The ActualAssessment will be attached to the context and automatic relationship fixup will popolate TheEvent.ActualAssessment if

    • you don’t disable change tracking
    • the relationship is not many-to-many

    As a side note: You can use AsEnumerable() instead of the first ToList() to avoid the unnecessary overhead of creating a list of anonymous objects.

    Edit

    For a many-to-many relationship or in case of disabled change tracking you must set the navigation property after the DB query is materialized, like so for example:

                .Select(evntInfo => {
                    evntInfo.TheEvent.ActualAssessment = evntInfo.ActualAssessment;
                    return new ExtendedEvent {
                        TheEvent = evntInfo.TheEvent,
                        SomeExtraData = evntInfo.SomeExtraData
                    };
                })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a web service that often scrapes data from remote web pages. After
I often find myself with fairly complex data that represents something that my objects
I'm writing a system at the moment that needs to copy data from a
We often use quick one-off SQL files to insert or update data in an
In my project, we use web services to get data from the DB and
I often use Ctrl + P to open files, but then I'd like to
I often use the string search feature for an entire solution in VS2008. Is
I often use the net user command to have a look at AD groups
I often use references to simplify the appearance of code: vec3f& vertex = _vertices[index];
I often use convenience functions that return pointers to static buffers like this: char*

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.