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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:39:47+00:00 2026-05-21T06:39:47+00:00

I recently refactored a WPF app so that it no longer wraps each use

  • 0

I recently refactored a WPF app so that it no longer wraps each use of the DbContext in a using clause (see this question). Instead, my app just uses the same DbContext singleton throughout.

This works great except for one small problem. I have a routine that rebuilds the database from scratch and inserts some default data. This routine uses ADO.NET directly (not the DbContext), so the DbContext is unaware that the database is now completely different.

Is there a method to reset the DbContext without disposing it? I’d like to avoid disposing if possible because this would break several references to the original singleton throughout the app.

  • 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-05-21T06:39:48+00:00Added an answer on May 21, 2026 at 6:39 am

    I was not able to come up with a method to reset the global DbContext. I was able to solve my problem, however, by injecting a DbContextLocator into any class that needs a DbContext instead of passing the DbContext itself.

    My goal was to maintain a global DbContext, but allow it to be reset whenever needed (such as after a database rebuild or import).

    My solution uses an abstract base class and a concrete class.

    Base Class

    using System.Data.Entity;
    
    namespace CommonLibrary.Database
    {
        public abstract class DbContextLocator
        {
            private DbContext _dbContext;
    
            public DbContext Current
            {
                get { return _dbContext; }
            }
    
            public DbContextLocator()
            {
                _dbContext = GetNew();
            }
    
            public virtual void Reset()
            {
                _dbContext.Dispose();
                _dbContext = GetNew();
            }
    
            protected abstract DbContext GetNew();
        }
    }
    

    Concrete Class

    using System.Data.Entity;
    using CommonLibrary.Database;
    using ExperimentBase.EntityModels;
    
    namespace MainProject.Models    
    {
        public class MainDbContextLocator : DbContextLocator
        {
            public new MainDbContext Current
            {
                get { return (MainDbContext)base.Current; }
            }
    
            protected override DbContext GetNew()
            {
                return new MainDbContext();
            }
        }
    }
    

    Usage

    Get the current DbContext:

    var dbContext = dbContextLocator.Current;
    

    Reset the DbContext:

    dbContextLocator.Reset();
    //Note: normally followed by code that re-initializes app data
    

    Edit

    Based on Shimmy’s feedback, I made DbContextLocatorBase into a generic. (I’m also now implementing IDisposable.)

    Base Class

    public class DbContextLocator<TContext> : IDisposable
        where TContext : DbContext, new()
    {
        private TContext _dbContext;
    
        public TContext Current
        {
            get { return _dbContext; }
        }
    
        public DbContextLocator()
        {
            _dbContext = GetNew();
        }
    
        public virtual void Reset()
        {
            _dbContext.Dispose();
            _dbContext = GetNew();
        }
    
        protected virtual TContext GetNew()
        {
            return new TContext();
        }
    
        public void Dispose()
        {
            _dbContext.Dispose();
        }
    }
    

    Concrete Class (optional, since the base class is no longer abstract)

    public class MainDbContextLocator : DbContextLocator<MainDbContext> { }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently refactored code like this ( MyClass to MyClassR ). #include <iostream> class
I've recently refactored an existing CodeIgniter application to use url segments instead of query
Recently I'm doing some work on RTMP streaming, that is using Flowplayer to integrate
I have a DataTrigger that I recently refactored. It used to have the DataContext
Recently i am making an app on facebook. So i have use facebook api.
I have recently started using generics in java, and in that attempt tried to
Recently I found about this tool easy_install that help me to easy install additional
Recently I was confused by this question . Maybe because I didn't read language
I am using a webgrid to display some dynamic data. I have recently refactored
I'm refactoring some code that a friend wrote and recently stumbled across this function:

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.