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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:28:52+00:00 2026-05-24T16:28:52+00:00

I am using ASP.Net / WebForms / Entity Model / Framework 3.5 Here is

  • 0

I am using ASP.Net / WebForms / Entity Model / Framework 3.5

Here is my project’s simple structure
Forms > BLL > DAL ( uses entity model )

Here is my DAL’s snippet

public class MyDAL : IDisposable
{
    private MyEntities db;
    public BaseDAL()
    {
        db = new MyEntities();
    }

    public User GetUserByID(int userId)
    {
        try
        {
            IQueryable<User> objUser = null;
            objUser  = from res in db.Users
                          where res.UserId == userId
                          select res;

            return objUser.FirstOrDefault();
        }
        catch
        {
            throw;
        }
    }

    public void Dispose()
    {
        db.Dispose();
    }
}

I call the DAL’s function from my BLL like this

public class MyBLL
{
    public User GetUserByID(int userId)
    {
        try
        {
            using (MyDAL objMyDAL = new MyDAL())
            {
                return objMyDAL.GetUserByID(userId);
            }
        }
        catch
        {
            throw;
        }
    }
}

I am calling the DAL through using block so MyDAL’s Dispose event will fire soon after the BLL returns the User object. So at this point ObjectContext instance gets disposed.

Now in my Web Form, I am calling this function like this to get user information and Group details which is a foreign key of user_Group Table in User table

    protected void Page_Load(object sender, EventArgs e)
    {
        MyBLL objMyBll = new MyBLL();

        User objUser = objMyBll.GetUserByID(123);
        objUser.User_GroupReference.Load(); // ERROR LINE
        int groupId = objUser.User_Group.Group_Id;
    }

When the ode comes on line objUser.User_GroupReference.Load(); I get this exception

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

How to resolve this? If I do not do db.Dispose(); in my DAL’s dispose method, it works fine and no exceptions comes. But if I do not dispose the db object there, when & where should I dispose it?
And how to access Reference Keys after disposed object context?

  • 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-24T16:28:52+00:00Added an answer on May 24, 2026 at 4:28 pm

    The exception is fired because lazy loading is fired when you access that navigation property but lazy loading works only within scope of context used to load the entity. If you dispose the context you will lose lazy loading ability. There is no way to use lazy loading after context disposal (except attaching the entity to the new context but it will only work if you detach it from the original context before you dispose it).

    In your architecture you must use Include to explicitly load every relation you will need in upper layers. If you want to use lazy loading your context must live for the whole duration of the request. In case of web forms it can be handled for example in BeginRequest and EndRequest event handlers where you create context in BeginRequest and dispose it in EndRequest. The context will be stored in HttpContext.Items. You should get the context from this collection (you can make helper method for that) and pass it to constructor of BLL which will in turn pass it to DAL. Don’t access HttpContext.Items from BLL or DAL.

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

Sidebar

Related Questions

I'm using the Entity Framework as my ORM for an ASP.Net (webforms) application. Suppose
I've been a longtime ASP.NET developer in the web forms model, and am using
I am working with VS 2010, Entity framework, SQl-Server 2005, ASP.Net web forms .
I've recently been developing a website using asp.net webforms that uses in proc sessions
I got a ASP.Net webforms app which is using Forms authentication. It needs to
I use C# Entity Framework and Asp.net Web Forms 4. At the moment I'm
Currently I'm working on Web project that is built using ASP.NET Web Forms. We
I am working on website using ASP.Net C# WebForms Framework 3.5 I am using
So, I've spent enough time using ASP.NET webforms to know that I'd almost rather
I am building a asp.net webforms (3.5 sp1) application, using jquery where I can

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.