How to refresh DB connection in a WPF app using MVVM pattern with Entity Framework and Oracle as DB?

Santhosh Badam 40 Reputation points
2024-08-21T04:54:32.44+00:00

I have a WPF application that uses Entity Framework and Oracle as the database and follows the MVVM pattern. Each ViewModel initializes its own DBContext and uses it.
When I click a button/link then, it refresh the DB connection. Also how to add the back up databases to this application. Can anyone provide guidance on how to achieve this?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,783 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,008 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
814 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hongrui Yu-MSFT 2,465 Reputation points Microsoft Vendor
    2024-08-21T08:17:37.02+00:00

    Hi,@Santhosh Badam. Welcome to Microsoft Q&A. 

    When you click a button/link, refresh the database connection. You could refer to the following method to do so.

    
    <Button Click="Button_Click" Content="Refresh Connection" Height="50" Width="200"></Button>
    
    

    MainWindow

    
        public partial class MainWindow : Window
    
        {
    
            MainWindowViewModel mainWindowViewModel = new MainWindowViewModel();
    
            public MainWindow()
    
            {
    
                InitializeComponent();       
    
                this.DataContext = mainWindowViewModel;
    
            }
    
     
    
            private void Button_Click(object sender, RoutedEventArgs e)
    
            {
    
                mainWindowViewModel.Refresh_Connection();
    
            }
    
        }
    
    

    MainWindowViewModel

    
        public class MainWindowViewModel
    
        {
    
            private static readonly MyDbContext dbContext = new MyDbContext();
    
     
    
            public void Refresh_Connection()
    
            {
    
                dbContext.Database.Connection.Close();
    
                dbContext.Database.Connection.Open();
    
            }
    
        }
    
    
    

    Load the backup database. You could create another dbcontext class and use the dbcontext to use the backup database.

    If the structure of the backup database is the same as that of the original database, you could consider only modifying the connection string to use the backup database

    dbContext.Database.Connection.ConnectionString = "Back up the database connection string";
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.