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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:59:58+00:00 2026-06-12T21:59:58+00:00

Is it necessary to control the amount of dbContext objects that are being created?

  • 0

Is it necessary to control the amount of dbContext objects that are being created?

I thought it might be safer to apply the Singleton pattern to my customDbContext class. However, when I execute the Update-Database command in the Package Manager Console I get an error that my dbContext does not have a default constructor (due to me implementing the Singleton pattern).

Question: Is it really that much safer or should I go for the simple solution?

public class WebshopContext : DbContext
{
    private static WebshopContext database;

    private WebshopContext() : base("DefaultConnection") { }

    public static WebshopContext GetInstance() {
        if (database != null)
        {
            return database;
        }
        else {
            database = new WebshopContext();
            return database;
        }
    }
}
  • 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-12T21:59:59+00:00Added an answer on June 12, 2026 at 9:59 pm

    DbContext corresponds to a unit of work. You should not create a singleton as that would be shared among all your request. You should not create a new DbContext for each database request since different database requests in a single unit of work (could be a method in your controller) should share the same entities to be compatible.

    Create one DbContext for each command in your controller and pass that around, when the request is complete you submit changes if it is an update request.

    If you don’t want to pass your DbContext around all the time you could create a “scope” for your DbContext.

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

Sidebar

Related Questions

I have developed an MFC ActiveX control and created necessary cab files and inf
Do you keep tools that are necessary to build your project under version control?
Is it necessary that the assignment operator ( = ) always copies the rvalue
Source control is great. One of the nice features is that I know that
I have a homemade toolbar control that is used in several places, both in
I am attempting to create a custom server control CollapsablePanel that extends ASP.net's Panel.
I have a grid control that I use throughout the application. I would like
I have a project that I'm working on that has a TreeView Control in
I've written a plug-in that constructs a rich UI control. I'm hoping that there's
I have built a User Control library that has a reference to a WCF

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.