How to refresh the database connection in WPF with Entity framework having Oracle database in MVVM?

SANTHOSH B 20 Reputation points
2024-01-24T05:38:28.6033333+00:00

I have a WPF with Entity framework having Oracle database also follows MVVM pattern. So I need to create a link such a way that if on click the link that refresh the database connection means need to reconnect to the database. Please help me for 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,703 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Olaf Helper 42,746 Reputation points
    2024-01-24T06:47:55.8166667+00:00
    0 comments No comments

  2. Hui Liu-MSFT 47,176 Reputation points Microsoft Vendor
    2024-01-24T08:28:30.12+00:00

    Hi,@ SANTHOSH B. Welcome to Microsoft Q&A.

    You could try to see if DbEntityEntry.Reload Method is what you want.

    User's image Or refreshing data after fetching:

    private void RefreshData(DBEntity entity)
    {
        if (entity == null) return;
    
        ((IObjectContextAdapter)DbContext).ObjectContext.RefreshAsync(RefreshMode.StoreWins, entity);
    }
    
    private void RefreshData(List<DBEntity> entities)
    {
        if (entities == null || entities.Count == 0) return;
    
        ((IObjectContextAdapter)DbContext).ObjectContext.RefreshAsync(RefreshMode.StoreWins, entities);
    }
    

    Additionally, you can see if the following example fits your needs.

    Here is a simplified example that demonstrates the steps to refresh a database connection in a WPF application using Entity Framework involving an Oracle database in MVVM pattern.

    App.config:

    <configuration>
      <connectionStrings>
        <add name="OracleDbContext" 
             connectionString="Data Source=YourInitialDataSource;User Id=YourInitialUserId;Password=YourInitialPassword;" 
             providerName="..." />
      </connectionStrings>
    </configuration>
    
    

    DbContext:

    using System.Data.Entity;
    
    public class OracleDbContext : DbContext
    {
        public OracleDbContext(string connectionString) : base(connectionString)
        {
        }
    
        public DbSet<Customer> Customers { get; set; }
        // ... other DbSet properties and configurations
    }
    
    

    ViewModelBase:

    
    using System.ComponentModel;
    
    public class ViewModelBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    
    

    MainViewModel:

    
    public class MainViewModel : ViewModelBase
    {
    
    private OracleDbContext dbContext;
    
    public MainViewModel()
    {
        // Initialize DbContext with the default connection string
        dbContext = new OracleDbContext("OracleDbContext");
    }
    
    public void RefreshDatabaseConnection()
    {
        // Get the new connection string (for simplicity, assuming it's hard-coded here)
        string newConnectionString = "Data Source=YourNewDataSource;User Id=YourNewUserId;Password=YourNewPassword;";
    
        // Dispose the existing DbContext
        dbContext.Dispose();
    
        // Initialize a new DbContext with the updated connection string
        dbContext = new OracleDbContext(newConnectionString);
    
        // Notify UI elements that depend on the DbContext to refresh
        OnPropertyChanged(nameof(CustomerData));
    }
    
    public IEnumerable<Customer> CustomerData => dbContext.Customers.ToList();
    // ... other ViewModel logic
    }
    
    

    MainWindow.xaml:

    
    <Window x:Class="YourNamespace.MainWindow"
           ...
            Title="MainWindow" Height="350" Width="525">
       <Window.DataContext>
            <local:MainViewModel/>
        </Window.DataContext>
    
        <Grid>
            <DataGrid ItemsSource="{Binding CustomerData}" AutoGenerateColumns="True"/>
            <Button Content="Refresh Connection" Command="{Binding RefreshCommand}" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10"/>
        </Grid>
    </Window>
    
    

    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.


  3. Santhosh Badam 20 Reputation points
    2024-06-18T10:28:52.8366667+00:00

    I have a Oracle19c database is connected to 4 machines having WPF applications with MVVM pattern and follows entity frame work. So from this following query
    SELECT MACHINE FROM v$session WHERE USERNAME = 'C##SAN' AND STATUS = 'ACTIVE';

    and also using different parameters able to the other information.

    Need to create a link by clicking the link the database connection for all the machines should refresh. How can I achieve . Please help me for this.

    0 comments No comments

  4. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more