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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:30:45+00:00 2026-05-31T13:30:45+00:00

I am getting this error: The ObjectContext instance has been disposed and can no

  • 0

I am getting this error:

The ObjectContext instance has been disposed and can no 
longer be used for operations that require a connection.

I understand why I am getting the error. However, what I do not understand is why one situation will cause the error while another will not. Here are the two situations.

Case One (causes error):

        List<SomeObject> someobjects;
        using (var gm = new GenericRepository<SomeObject>())
        {
            someobjects = gm.Get().ToList();
        }
        vm.SomeObjectSelectList = slf.getSpecificList(someobjects);

What is happening here is a List<> is being filled from a database using a generic repository. After that, the list is then sent to a factory (slf) for creating select lists that are used in the view model (vm). When used in this fashion, the error above occurs. The reason is that inside of the factory there is this line of code:

w => w.Date + " " + w.Child.FirstName + " " + w.Child.LastName);

Although the list of someobjects is sent just fine, its nested objects are not, and when the call to .Child is called the db context is called and the error is caused.

Case Two (causes no error):

        List<SomeObject> someobjects;
        using (var gm = new GenericRepository<SomeObject>())
        {
            someobjects = gm.Get().ToList();
            vm.SomeObjectSelectList = slf.getSpecificList(someobjects);
        }

In this case, no error is caused. However, from the first case it is obvious that the database context is being contacted.

How does the factory have access to the context when the context is inside of the repository?

  • 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-31T13:30:45+00:00Added an answer on May 31, 2026 at 1:30 pm

    The factory doesn’t have access to the context – it is someobjects that internally uses the context to retrieve the property values when you first access them. Since your context is disposed after the using block, trying to access the properties then will throw an exception.

    You can imagine e.g. the Child property implementation to be something like this:

    private Person _child = null;
    public Person Child
    {
        get
        {
            return _child ?? GetandSetChildFromContext();
        }
    }
    

    The value of Child is retrieved lazily from the context only when needed – if you want to eagerly include related properties use an Include() query – this is advisable i.e. if you know already that you will need certain related properties every time. This is somewhat made difficult by the fact that you use a repository layer on top of EF – make sure that repository supports Include queries.

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

Sidebar

Related Questions

so, I'm getting a The ObjectContext instance has been disposed and can no longer
Getting this error with jquery & jquery.form. Site has been live for awhile..upgraded to
I am getting this error now that I hit version number 1.256.0: Error 4
Im getting this error Internal zval's can't be arrays, objects or resources in Unknown
Getting this error: Each GROUP BY expression must contain at least one column that
Getting this error on issue mysql command on Ubuntu machine: ERROR 2002 (HY000): Can't
Getting this error: 2009-09-03 12:44:02.307 xcodebuild[307:10b] warning: compiler 'com.apple.compilers.llvm.clang.1_0.analyzer' is based on missing compiler
Anyone getting this error when using the new free chart controls MS bought from
I´m getting this error while trying to commit to a svn repository: svn: MKACTIVITY
Keep getting this error after inserting a subdatasheet into a query and trying to

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.