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 8994333

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:24:27+00:00 2026-06-15T23:24:27+00:00

I am using a code-first approach with Entity Framework, and a repository pattern to

  • 0

I am using a code-first approach with Entity Framework, and a repository pattern to get entities back from my database. In my data model, each OverallEvent has many EventInConcept children. I want my GetEvents method to return an IList of OverallEvents, and I want the children of the aforementioned relationship to be concretized such that they can be accessed outside my DbContext (which AssessmentSystemContext is). This is the code I currently have:

public IList<OverallEvent> GetEvents() {
    using (var context = new AssessmentSystemContext()) {
        return context.OverallEvents
            .Select(evnt => new {
                OverallEvent = evnt,
                // evnt.EventsInConcept is a public virtual ICollection<EventInConcept>
                ConcreteEventsInConcept = evnt.EventsInConcept
            })
            .AsEnumerable()
            .Select(evntData => {
                evntData.OverallEvent.EventsInConcept = evntData.ConcreteEventsInConcept.ToList();
//              foreach (var eic in evntData.OverallEvent.EventsInConcept) {
//                  eic.Name = eic.Name;
//              }
                return evntData.OverallEvent;
            })
            .ToList();
    }
}

It gives me back a list of OverallEvent entities, which is fine, but the trouble is that if I try to access the child relationship EventsInConcept, I get an error. For example:

EventRepository repoEvent = new EventRepository();
var gotEvents = repoEvent.GetEvents();
var firstEventInConcept = gotEvents[0].EventsInConcept.FirstOrDefault();

… gives me the error “The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.”

I understood from the answer to an earlier question that if I projected EventsInConcept into a wrapper object, then explicitly set it in a later .Select call (ie. evntData.OverallEvent.EventsInConcept = evntData.ConcreteEventsInConcept.ToList();), it would concretize this one:many relationship and I would be able to access EventsInConcept outside of the DbContext, but it isn’t working here. Note that if I uncomment the foreach loop, it starts working, so to get it to work I have to explicitly set a property on every single entry of EventsInConcept. I don’t really want to have to do this (I’m picking an arbitrary property, .Name, which feels wrong anyway). Is there a better way?

  • 0 0 Answers
  • 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-15T23:24:29+00:00Added an answer on June 15, 2026 at 11:24 pm

    Disable lazy loading for this query. It is of no use in that situation and when you dispose the context after the entities have been retrieved:

    public IList<OverallEvent> GetEvents() {
        using (var context = new AssessmentSystemContext()) {
            context.Configuration.LazyLoadingEnabled = false;
            return ...
        }
    }
    

    It might be possible that EF doesn’t recognize that the collection has been loaded when you use a projection (instead of eager or explicit loading) and triggers lazy loading as soon as you access the collection.

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

Sidebar

Related Questions

I am successfully creating database (SQL Ce) using Entity Framework Code First approach (C#-WPF).
I'm using the Entity Framework 4 code first approach to design my database in
What are the pros & cons of using Entity Framework 4.1 Code-first over Model/Database-first
I am trying to update my entity using Entity Framework 4.1 code First approach
I am using the latest Entity Framework with a code-first approach to create a
I'm using ASP.NET MVC 3 with the Entity Framework 4 code first approach and
I'm building an ASP.NET MVC 3 site using the code-first Entity Framework 4 approach.
I've successfully mapped an Entity Framework Code-First data model with an existing Sql Server
I have the following table created using Entity Framework Code First approach. How do
I'm building a layered application using Entity Framework code first approach with an mvc4

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.