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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:42:38+00:00 2026-06-14T04:42:38+00:00

I am having an issue that I believe can be resolved with a simple

  • 0

I am having an issue that I believe can be resolved with a simple understanding. I am using Entity Framework 5 with code first and POCO. I have all navigational properties properly configured (virtual) for all of my POCO objects. The problem occurs when I query for an object (POCO) and then return that POCO as a result. All navigational properties for the POCO are null’ed:

class PocoParent  { // all of these are properties (get/set)
    public int Id;
    public string ParentProperty;
    public virtual PocoChild NavProperty;
}
class PocoChild {
    public int Id;
    public int ParentId;
    public string Name;
    public virtual PocoParent Parent;
}

Inside a repository class that handles my queries:

IEnumerable<PocoChildren> GetAllChildrenFor(int parentId) {
    using(Context db = new Context()) {
        var pcs = db.PocoParents.Where(p => p.Id == parentId);
        return pcs;
    }
}

Now use the repository:

...
var children = repo.GetAllChildrenFor(queriedParent.Id);
...

Now use the results from the repository and this is where the error occurs:

...
foreach(child in children) {
   if(child.Parent.NavProperty == "null") {  !!! Exception: child.Parent ObjectContext already disposed
   }
}
...

How can I dispose the ObjectContext (to detach the POCO object), but retain at least one level of navigational properties? I have looked endlessly for a solution, but I am stumped as the solutions are all conflicting one another on how to do it.

— Recap —-
With the answer given below, if I were to change the query in the repository to the following:

IEnumerable<PocoChildren> GetAllChildrenFor(int parentId) {
    using(Context db = new Context()) {

        var pcs = db.PocoParents.Include(p => p.Select(prop => prop.Parent).Where(p => p.Id == parentId);

        return pcs;
    }
}

Would that return the all the enities and they will contain a non-null .Parent property, or whatever property I specify?

  • 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-14T04:42:39+00:00Added an answer on June 14, 2026 at 4:42 am

    Lazy loading is enabled by default and that is why the navigable properties are null. Lazy loading means the navigable properties aren’t loaded until they are requested. And if the context is gone when they are requested, they get set to null since they can’t be loaded.

    To get around this, you need to disable lazy loading or explicitly (and eagerly) load the properties you need.

    This MSDN magazine article is a good source to help you decide which route is best for you.

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

Sidebar

Related Questions

I'm having an issue that I think is simple to diagnose, but I can't
Having an issue here that I have tried everything I can think of but
I am having an issue with somethign that I thoguth woudl have been simple.
I can't believe that I'm having an issue with this. My setTimeout is: setTimeout(function(){showContent(entries,
I am having an issue that I can't seem to figure out. Hopefully somebody
I am having a strange issue that I can't seem to resolve. Here is
I am having a JQuery/ASP issue that I hope someone can help me with.
I am having an issue with many-to-many mapping using NHibernate. Basically I have 2
We have been having this issue pop up sporadically, but now I can reproduce
I having issue that content assistant / intellisense is working in methods such as

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.