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

  • Home
  • SEARCH
  • 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 7506007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:07:02+00:00 2026-05-29T22:07:02+00:00

I have an Entity Framework class called Student. It has a list of Classes.

  • 0

I have an Entity Framework class called Student. It has a list of Classes. Now, everytime I (from my view) refer to ViewBag.Student.Classes, it fails with an object disposed exception, because my controller looks like this:

using(var myObjectContext = new ModelContainer()) {
    ViewBag.Student = myObjectContext.UserSet.OfType<Student>().FirstOrDefault(s => s.Id == ActiveStudentSignedIn);
}

Now, I know I could just pass in the student’s classes in the viewbag too, but that would increase complexity when using strong-typed partial views that rely on a student and its details.

I think it would be wrong to not having the “using” keyword there, because wouldn’t that leave up too much garbage for the garbage collector to collect?

I’m obviously doing something wrong. But what?

  • 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-29T22:07:03+00:00Added an answer on May 29, 2026 at 10:07 pm

    There are two ways to solve this:

    • Use eager loading (Include method in query) and pass all data you need in view from your controller action directly. Currently you receive exception because your view is trying to trigger lazy loading and execute another queries to database but the context is already disposed.
    • Extend lifetime of your context = don’t create context per action. Create single context per controller (new instance is created for every request) and dispose context in controller’s Dispose method. You can also inject the context into controller from outside – dependency injection but that would require some other infrastructure.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Application class. Entity framework created a navigation property called Assistants .
I have an entity class called catObras, and it inherits from the class NSManagedObject.
We have an entity framework model with has some inheritance in it. The following
I have finally decided to go with the Entity Framework since it has the
I have a problem with the following Linq query using Entity Framework: from o
I have an Entity Framework 4.0 Entity Object called Revision w/ Nullable DateEffectiveFrom and
I am attempting to retrieve a list of objects from Entity Framework via WCF,
I have an Entity Framework POCO class that is generated by a T4 Template.
I have 2 projects - a class library containing an EDM Entity Framework model
I am using entity framework 4.1 code first. I have a GrantApplication class: public

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.