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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:49:15+00:00 2026-06-14T21:49:15+00:00

I am currently using a DbContext similar to this: namespace Models { public class

  • 0

I am currently using a DbContext similar to this:

namespace Models
{
    public class ContextDB: DbContext
    {
              
        public DbSet<User> Users { get; set; }
        public DbSet<UserRole> UserRoles { get; set; }

        public ContextDB()
        {
            
        }
    }
}

I am then using the following line at the top of ALL my controllers that need access to the database. Im also using it in my UserRepository Class which contains all methods relating to the user (such as getting the active user, checking what roles he has, etc..):

ContextDB _db = new ContextDB();

Thinking about this, there are occasions when one visitor can have multiple DbContexts active, for instance if it is visiting a controller that uses the UserRepository, which might not be the best of ideas.

When should I make a new DbContext? Alternatively, should I have one global context that is passed around and reused in all places? Would that cause a performance hit? Suggestions of alternative ways of doing this are also welcome.

  • 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-14T21:49:16+00:00Added an answer on June 14, 2026 at 9:49 pm

    I use a base controller that exposes a DataBase property that derived controllers can access.

    public abstract class BaseController : Controller
    {
        public BaseController()
        {
            Database = new DatabaseContext();
        }
    
        protected DatabaseContext Database { get; set; }
    
        protected override void Dispose(bool disposing)
        {
            Database.Dispose();
            base.Dispose(disposing);
        }
    }
    

    All of the controllers in my application derive from BaseController and are used like this:

    public class UserController : BaseController
    {
        [HttpGet]
        public ActionResult Index()
        {
            return View(Database.Users.OrderBy(p => p.Name).ToList());
        }
    }
    

    Now to answer your questions:

    When should I make a new DbContext / should I have one global context
    that I pass around?

    The context should be created per request. Create the context, do what you need to do with it then get rid of it. With the base class solution I use you only have to worry about using the context.

    Do not try and have a global context (this is not how web applications work).

    Can I have one global Context that I reuse in all places?

    No, if you keep a context around it will keep track of all the updates, additions, deletes etc and this will slow your application down and may even cause some pretty subtle bugs to appear in your application.

    You should probably chose to either expose your repository or your Context to your controller but not both. Having two contexts being access from the same method is going to lead to bugs if they both have different ideas about the current state of the application.

    Personally, I prefer to expose DbContext directly as most repository examples I have seen simply end up as thin wrappers around DbContext anyway.

    Does this cause a performance hit?

    The first time a DbContext is created is pretty expensive but once this has been done a lot of the information is cached so that subsequent instantiations are a lot quicker. you are more likely to see performance problems from keeping a context around than you are from instantiating one each time you need access to your database.

    How is everyone else doing this?

    It depends.

    Some people prefer to use a dependency injection framework to pass a concrete instance of their context to their controller when it is created. Both options are fine. Mine is more suitable for a small scale application where you know the specific database being used isn’t going to change.

    some may argue that you can’t know this and that is why the dependency injection method is better as it makes your application more resilient to change. My opinion on this is that it probably won’t change (SQL server & Entity Framework are hardly obscure) and that my time is best spent writing the code that is specific to my application.

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

Sidebar

Related Questions

Currently using the HTTPServletRequest class and specifically the .getQueryString method to retrieve an inputted
Currently using MySQL version 5.1.6 This is my first real world build and so
I'm using the DbContext class within code that I am creating that is based
I am having this kind of mysterious issue here. I am currently using Entity
First, I have a dbcontext factory which is defined public class DatabaseFactory : Disposable,
I'm currently using DbContext with Ef 4.1, and I'm trying to audit all changes
I'm using EF4.1 POCO. I have two tables [Table(Parent)] public class Parent { public
Normally the code for using the dbContext from Entity Framework looks something like this:
Currently using Google Analytics as a supplement to our paid tracking software, but neither
Currently using Xcode 4.2 and I have two view controllers (1 and 2). 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.