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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:42:09+00:00 2026-06-08T02:42:09+00:00

I used Entity Framework to create my database schema and generate my code. I

  • 0

I used Entity Framework to create my database schema and generate my code. I have a table called Employee that has child records in a DaysOff table. DaysOff has a foreign key to Employee and there is a 1 to * association in my model. I ran a LINQ query on the Employee table and expected that my Domain.Employee object would be populated with the DaysOff but the DaysOff are null. When I drill down in the Object I see “employee.DaysOff.Count threw an exception of type System.ObjectDisposedException”. Am I wrong to think that the child records will be populated? How would I do this? Here is the method I call to get my Employee:

public static Domain.Employee SelectEmployee(int employeeId)
{
    using (var db = new EmployeeEntities())
    {

        Domain.Employee emp = (from e in db.Employees
                       where e.EmployeeId == employeeId
                       select e
                             ).FirstOrDefault();

        return emp;
    }
}

EDIT:
A combination of the accepted answer below and the comments (all up-voted) helped me to solve this (yay!):

public static Domain.Employee SelectEmployee(int employeeId)
{
    using (var db = new EmployeeEntities())
    {

        Domain.Employee emp = (from e in db.Employees.Include("DaysOff")
                       where e.EmployeeId == employeeId
                       select e).FirstOrDefault();

        return emp;
    }
}
  • 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-08T02:42:10+00:00Added an answer on June 8, 2026 at 2:42 am

    Am I wrong to think that the child records will be populated?

    I’m guessing that it could be that the DaysOff is populated lazily, but by that point EmployeeEntities has been disposed. You might want to try something like:

    using (var db = new EmployeeEntities().Include("Employee.DaysOff"))
    

    Also note that your code in the using statement would be simpler written as:

    return db.Employees.FirstOrDefault(e => e.EmployeeId == employeeId);
    

    Edit

    The code above is not correct. Include must be used on an ObjectQuery<T> or IQueryable<T> and can’t be applied to an ObjectContext/DbContext. Correct usage is:

    using (var db = new EmployeeEntities())
    {
        return db.Employees.Include("DaysOff")
            .FirstOrDefault(e => e.EmployeeId == employeeId);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why does entity framework generate Create method for entities that takes parameter for the
I am developing a rather large database schema using Entity Framework Code First. I
I used JPA and mysql. Each table have entity class.I have four tables. Table1
Is it possible to create the database from the classes with entity framework 4.0?
I have a situation where I have a table that has a soft foreign
I have been using Entity Framework 4.3 on an existing database and I have
I'm using Entity Framework, and I have a COMMENT entity. A COMMENT has a
I've recently started using Entity Framework migrations and noticed that the database name is
I have my asp.net mvc 3 application with entity framework and i used the
i separated my application into a DAL, BL, UI. I used entity framework code

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.