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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:03:45+00:00 2026-06-09T16:03:45+00:00

I have seen many examples using Entity Framework in MVC3 applications, they are very

  • 0

I have seen many examples using Entity Framework in MVC3 applications, they are very simple demos which only have one mvc3 web project with edmx inside it.

So, they can use the best practice for open and close connection by “using” statement:

using(var context = new SchoolEntities())
{
    // do some query and return View with result.
}

And, It can use lazy load (navigation properties) inside the “using” statment correctly, because the context is not yet
disposed:

foreach(var item in student.Course)
{
    // do something with the navigation property Course
}

All things seems to be perfect until it becomes an n-tier application.

I created DAL, BLL, and a MVC3 UI.

The DAL have edmx inside it, and operator classes like SchoolDA.cs:

public class StudentDA()
{
    public Student FindStudent(int studentId)
    {
        using(var context = new SchoolContext())
        {
            // do query, return a student object.
        }
    }
}

Then, in the BLL, if I use:

var student = studentDa.FindStudent(103);

then invoke it’s navigation property:

student.Course

I will get an error (of course):

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

So, I have to change StudentDA.cs like this:

public class StudentDA() : IDisposable
{
    private SchoolEntites context;

    public StudentDA()
    {
        context = new SchoolEntities();
    }

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

    public Student FindStudent(int studentId)
    {
        // do query, return a student object.
    }
}

Then, the BLL will change like this:

public Student FindStudent(int id)
{
    using(var studentDa = new StudentDA())
    {
        // this can access navigation properties without error, and close the connection correctly.
        return studentDa.FindStudent(id);
    }
}

All things seem to be perfect again until it meet Update() method.

Now, if I want to update a student object which is taken from BLL.FindStudent(), the context.SaveChanges() will return 0, because the context is already disposed in the BLL.FindStudent(), and nothing will be updated to database.

var optStudent = new StudentBO();
var student = optStudent.FindStudent(103);
student.Name = "NewValue";
optStudent.Update(student);

Does anyone have idea on how to use EntityFramework in 3 tire application? or how can I manage the context correctly. I will use navigation propertites very often in the web layer, but I can’t always remain connection open to consume the server memory.

  • 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-09T16:03:46+00:00Added an answer on June 9, 2026 at 4:03 pm

    There are multiple ways to handle EF context’s lifetime. In web apps, usually context is unique for an HttpRequest. For example, if you want to handle this manually in a web application and have a per Thread/HttpRequest EF context, you can do so with the following (Code copied from http://www.west-wind.com/weblog/posts/2008/Feb/05/Linq-to-SQL-DataContext-Lifetime-Management):

    internal static class DbContextManager
    {
        public static DbContext Current
        {
            get
            {
                var key = "MyDb_" + HttpContext.Current.GetHashCode().ToString("x")
                          + Thread.CurrentContext.ContextID.ToString();
                var context = HttpContext.Current.Items[key] as MyDbContext;
    
                if (context == null)
                {
                    context = new MyDbContext();
                    HttpContext.Current.Items[key] = context;
                }
                return context;
            }
        }
    }  
    

    And then you can easily use:

    var ctx = DbContextManager.Current
    

    But I suggest you leave the lifetime management to an IoC framework like Autofac, Castle Windsor, or Ninject which automatically handle the creation/disposal of your registered obejcts along with many other features.

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

Sidebar

Related Questions

I have seen many examples of using .NET Framework namespaces in c++/cli like the
I have seen many examples in Delphi for creating Email sending applications, where we
In many examples I have seen online, AsyncTask is extended, the constructor is overriden,
i have a general question about properties and ivars. ive seen many different examples
Greetings everybody. I have seen examples of such operations for so many times that
I've seen several examples of many to many relationships in EF using code-first, but
I have seen many examples of getting all permutations of a given set of
I have seen simple example Ajax source codes in many online tutorials. What I
I've been a little puzzled with this as I have not seen many examples
I am learning EF and have seen many examples, and during my learning I

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.