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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:08:30+00:00 2026-05-27T11:08:30+00:00

I have an MVC3 Project running with EF Code First. Here is my code

  • 0

I have an MVC3 Project running with EF Code First.

Here is my code for Home/Index:

public ActionResult Index()
{
    var IndVM = new IndexVM();
    using (QuoteContext QDomain = new QuoteContext())
    {
        IndVM.Quotes = QDomain.Quotes.Include("Tags").Include("Author").OrderByDescending(x => x.CreatedOn).Take(5).ToList();
        IndVM.Tags = QDomain.Tags.OrderByDescending(x => x.Quotes.Count).ToList();
        IndVM.Authors = QDomain.Authors.OrderByDescending(x => x.Quotes.Count).Take(5).ToList();
    }
    return View(IndVM);
}

As you can see I have the Querying stuff inside a using statement, and I am also calling the ToList(), but I still get the error:

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

Is this a bug in EF Code First?

  • 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-27T11:08:30+00:00Added an answer on May 27, 2026 at 11:08 am

    You have to turn off lazy loading, otherwise the serializer will try to traverse navigation properties and throw this exception.

    public ActionResult Index()
    {
        var IndVM = new IndexVM();
        using (QuoteContext QDomain = new QuoteContext())
        {
            QDomain.ContextOptions.LazyLoadingEnabled = false;
            // Query and populate IndVM here...
        }
        return View(IndVM);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an MVC3 project I created using the Code First paradigm and it
I have a new MVC3 project with one Controller called PublicController.cs which contains 4
First some context: I have an MVC3 .net project which, for the sake of
As I have been following a MusicStore tutorial, new MVC3 project created a following
When running my project I get a HttpCompileException was unhandled by user code message.
I have an MVC3 with nHibernate project, and I would like to make use
I have an asp.net mvc3 project, it has some reports in aspx web pages.
I am trying to write some unit tests for my MVC3 project (the first
I have 2 domains, .eu and .de and I have one mvc3 website running
I have created a MVC 3 WCF Rest project using the tutorial described here

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.