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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:25:50+00:00 2026-06-03T17:25:50+00:00

Is it correct to declare a global variable of DBContext in a controller and

  • 0

Is it correct to declare a global variable of “DBContext” in a controller and then use it for all database operations?

Example:

public class ProductController : Controller
{
    private readonly DBContextEntities _db = new DBContextEntities();

    public ActionResult Index()
    {
     var products = _db.Products.ToList();
     return View(products);
    }

    public ActionResult Create()
    {
     _db.Products.AddObject(new Product{Name="x",Price="5.2"});
     _db.SaveChanges();
     return View(products);
    }

}

Please Advice,

  • 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-03T17:25:51+00:00Added an answer on June 3, 2026 at 5:25 pm

    I have tried to weigh this up a few times. I’ve come to the conclusion that it is should be ok in the majority of situations. This is ‘why’ I think it should be ok.

    All the advice suggests keeping the context open for as little time as possible, this is to avoid tons of entities being loaded and held in memory.
    This would lead you to thinking that the context should be created and disposed in each method, rather than globally in a class.

    Since the duration of an HTTP request is small, having the context available globally shouldn’t be a major overhead, the resources needed to create the context each time would outweigh the benefits of keeping it open for the duration of the request.

    This answer is from a webforms perspective, I’m not 100% if an MVC controller would be kept alive for longer than a request and require a different response I’m afraid.

    I think the key is not to set the context as anything static, or something which is intended to live for the duration of the application, as this will lead you into massive memory consumption as the number of entities in memory grows.

    You could argue that not explicitly putting something which implements IDisposable inside a using block is a bad idea, which I’d agree with.

    (disclaimer: guesswork paragraph!)
    This is where I’m unsure of the possibility of resources being left open if the page chucks an exception or similar. I think 99.9% of the time you’d be fine, but there may be rare cases when the resource isn’t disposed of correctly

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

Sidebar

Related Questions

Correct me If Im wrong. public class Person{ List<Cars> myCars; //Get and Set }
consider this # global variable i_am_global = {} def get_global(): global i_am_global # declare
Possible Duplicate: why use IList or List? I have the following: public class CityViewModel
Correct me if anything is wrong. Now when we use Spring DAO for ORM
Am I correct in saying it is TSQL? I am a novice at database
I tried to declare a Boolean variable in a shell script using the following
My question might not be too correct... What I mean is: class MyClass {
I understand that the correct way to declare a return type const is to
What is the correct way to declare a multidimensional array and assign values to
Possible Duplicate: What is the correct way of using extern for global variables ?

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.