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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:42:46+00:00 2026-06-18T03:42:46+00:00

There a lot of code blocks like this: public class SomeController : Controller {

  • 0

There a lot of code blocks like this:

public class SomeController : Controller
{
    DbEntities entity = new DbEntities();

    public ActionResult Add()
    {
        entity.someOperations...
        return View();
    }

    public ActionResult Edit()
    {
        entity.someOperations...
        return View();
    }

    public ActionResult Detail()
    {
        entity.someOperations...
        return View();
    }

    public ActionResult Detail()
    {
        entity.someOperations...
        return View();
    }

    .....

Should I change the methods like this?:

public class SomeController : Controller
{
    public ActionResult Add()
    {
        using(DbEntities entity = new DbEntities())
        {
            entity.someOperations...
        }

        return View();
    }
    .....

What are the problems with not using using-statement in EF? OR what is the best way? Also, If we use using-statement code blocks are grown.

Thanks…

  • 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-18T03:42:47+00:00Added an answer on June 18, 2026 at 3:42 am

    The using statement approach is the best of the two that you have proposed above. Using this approach, you can be assured that the ObjectContext is closed and disposed of after it’s use.

    Using your other approach, one would assume that the ObjectContext could be left unclosed, therefore hogging connections to the database. To see this in action, try creating a sample app with your other approach, then profile it using the EFProfiler and watch the number of ObjectContext‘s opened rise, whilst the number of closures will be notably smaller.

    I recently worked on a project that was having database issues under high levels of usage that adopted your second pattern (you can see my question about it HERE). As I didn’t have enough time on the project/code base was too large, I didn’t have the option to switch to the using statement approach. Instead I implemented the following to manually force the disposal of the ObjectContext on the End_Request in Global.asax (I had an instance of the DbContext on a static instance of my BusinessLayerService:

    protected void Application_EndRequest(object sender, EventArgs e)
        {
            BusinessLayerService.Instance.Dispose();
            BusinessLayerService.Instance = null;
        }
    

    But if you have the option from the project start: I would highly recommend using the using pattern

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

Sidebar

Related Questions

In my code there are a lot of final values, like 10000. They never
Although there are a lot of jquery autocomplete code in this forum. However I
There are a lot of posts on here about this, I'm using this code
I wanted to write some code like this: class SomeModel(models.Model): field = models.ForeignKey(SomeOtherModel) def
I'm writing some code that has a lot of substitution. There's a list<char> productions
I know that there are lot's of questons on this, but all seem to
I know there a lot of similar questions to this, but I didn't find
A lot of times I like to write try catch blocks like so: try
There is a lot of code below but you dont have to really read
I'm reading through Windows Phone 7.5 Unleashed, and there is a lot of code

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.