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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:19:09+00:00 2026-06-01T11:19:09+00:00

Hi people am having a problem am getting the following error: The ObjectContext instance

  • 0

Hi people am having a problem am getting the following error:

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

and the error is directed at the following:

  @Html.DisplayFor(modelItem => item.tblConsole.ConsoleName) "within the game table"

I have the following code in my game controller:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GameTest.Models;

namespace GameTest.Controllers
{ 
    public class GameController : Controller
    {
        private gamezoneDBEntities db = new gamezoneDBEntities();

        //
        // GET: /Game/

        public ViewResult Index()
        {
            using (var db = new gamezoneDBEntities())
            {

                var Info = db.tblGames.Where(UserInfo => UserInfo.UserName.Equals(User.Identity.Name)).ToList();
                return View(Info);   

            }

        }

        //
        // GET: /Game/Details/5

        public ViewResult Details(int id)
        {
            tblGame tblgame = db.tblGames.Find(id);
            return View(tblgame);
        }

        //
        // GET: /Game/Create

        public ActionResult Create()
        {
            ViewBag.ConsoleNameIDFK = new SelectList(db.tblConsoles, "ConsoleName", "ConsoleName");
            return View(new tblGame { UserName = @User.Identity.Name });
        } 

        //
        // POST: /Game/Create

        [HttpPost]
        public ActionResult Create(tblGame tblgame)
        {
            if (ModelState.IsValid)
            {
                db.tblGames.Add(tblgame);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.ConsoleNameIDFK = new SelectList(db.tblConsoles, "ConsoleName", "ConsoleName", tblgame.ConsoleNameIDFK);
            return View(tblgame);
        }

        //
        // GET: /Game/Edit/5

        public ActionResult Edit(int id)
        {
            tblGame tblgame = db.tblGames.Find(id);
            ViewBag.ConsoleNameIDFK = new SelectList(db.tblConsoles, "ConsoleName", "ConsoleName", tblgame.ConsoleNameIDFK);
            return View(tblgame);
        }

        //
        // POST: /Game/Edit/5

        [HttpPost]
        public ActionResult Edit(tblGame tblgame)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tblgame).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ConsoleNameIDFK = new SelectList(db.tblConsoles, "ConsoleName", "ConsoleName", tblgame.ConsoleNameIDFK);
            return View(tblgame);
        }

        //
        // GET: /Game/Delete/5

        public ActionResult Delete(int id)
        {
            tblGame tblgame = db.tblGames.Find(id);
            return View(tblgame);
        }

        //
        // POST: /Game/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            tblGame tblgame = db.tblGames.Find(id);
            db.tblGames.Remove(tblgame);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}

I have used this code in the index so each game is unique to the user who posted the game:

        var Info = db.tblGames.Where(UserInfo => UserInfo.UserName.Equals(User.Identity.Name)).ToList();
        return View(Info);  

and have added this in the first create section of my controller:

return View(new tblGame { UserName = @User.Identity.Name });

This code does work when you don’t click on create and every user has individual posts of games but when I press create new it crashes

Any help would be appreciated. Please ask if you need to know more

  • 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-01T11:19:10+00:00Added an answer on June 1, 2026 at 11:19 am

    You need to Include the console table data in your query.

    var Info = db.tblGames.Include(x => x.tblConsole).Where(UserInfo => UserInfo.UserName.Equals(User.Identity.Name)).ToList();
                return View(Info); 
    

    Your returned entity is trying to lazy load in additional data but cannot because the data context has already been disposed. This is because you wrapped it in a using block.
    By using Include you are Eager Loading all the data you need in the initial query and the entity will not need to make any additional queries to get the data you are requesting in the view.

    You can read more here on Eager and Lazy loading: http://msdn.microsoft.com/en-us/library/bb896272.aspx

    Also, there is no reason to have the private instance of gamezoneDBEntities:

    private gamezoneDBEntities db = new gamezoneDBEntities()
    

    Just use the instance you create in the using block:

    using (var db = new gamezoneDBEntities())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having a bit of a problem getting my LINQ query to return the object
I'm sure someone can explain this. we have an application that has been in
I know that many people have had this problem... but I am now having
I've been having tons of problems getting the non-xml configuration for Castle Windsor set
I'm following a tutorial on php, and am having difficulty getting this to work.
I'm hoping non-IIS people can help me on this though the issue I'm having
I'm having a problem getting the right date format to be returned from my
I'm having a problem with getting the tutorial to work on the device it
I'm having a problem working in SharpDevelop using NHibernate and SQLite. I've seen people
I've searched around for people having similar problems, but to no avail. I'm trying

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.