Skip to content

How to change connection to the database at runtime from the login form in eXpressApp Framework

License

Notifications You must be signed in to change notification settings

DevExpress-Examples/xaf-how-to-change-connection-to-the-database-at-runtime

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

XAF - How to Change Connection to the Database at Runtime from the Login Form

Scenario

This example illustrates how to connect your application to a different database after the application start. Use this approach in a multi-tenant application where you need to associate every user or company with their own database. You can choose the user and the database during the login procedure. In this scenario, all databases have the same structure, but their predefined data sets may vary.

image

Alternative solutions: How to Implement a Multi-Tenant Application for Blazor and WinForms

Implementation Steps

  1. In the Solution Wizard, create a new XAF application and name it RuntimeDbChooser.

    • Use either of the available ORM libraries for data access: XPO or EF Core.
    • Select the Security module with the Authentication = Standard and Integrated Mode options.
  2. Copy the code that creates the predefined security users for each database from the RuntimeDbChooser.Module\DatabaseUpdate\Updater.xx file into the YourSolutionName.Module\DatabaseUpdate\Updater.xx file.

  3. Copy and include the RuntimeDbChooser.Module\BusinessObjects\CustomLogonParameters.xx file into the YourSolutionName.Module\BusinessObjects folder.

  4. For WinForms application only. Copy and include the RuntimeDbChooser.Module\ChangeDatabaseActiveDirectoryAuthentication.xx file into the YourSolutionName.Module project. For more information on this API, see the following article: How to: Use Custom Logon Parameters and Authentication.

  5. Copy and include the RuntimeDbChooser.Wxx\WxxApplicationEx.xx files into the YourSolutionName.Wxx project. Rename the RuntimeDbChooserWindowsFormsApplication, or RuntimeDbChooserAspNetApplication, or RuntimeDbChooserBlazorApplication to your WxxApplication descendant's name from the WxxApplication.xx file.

  6. Replace the line that instantiates your WinApplication descendant in the YourSolutionName.Win/Program.xx file with the CreateApplication method call as shown in the RuntimeDbChooser.Win/Program.xx file.

  7. Open the YourSolutionName.Web/WebApplication.xx file in the Application Designer. Select the Authentication Standard component and set its LogonParametersType property to RuntimeDbChooser.Module.BusinessObjects.CustomLogonParametersForStandardAuthentication.

  8. Replace the line that instantiates your BlazorApplication in the YourSolutionName.Blazor.Server/Startup.cs file and set the AddPasswordAuthentication.Options.LogonParametersType property to RuntimeDbChooser.Module.BusinessObjects.CustomLogonParametersForStandardAuthentication.

  9. Copy and include the CustomLogonController.cs file into the application project. Register this controller in the Application.CreateLogonController method override. The implementation of the controller in WinForms and ASP.NET WebForms applications differs from the implementation in Blazor Server applications.

Important Notes

  1. In this example, XAF Blazor applications load the available database names from the appsettings.json file.

  2. In WinForms and ASP.NET WebForms applications, the database names are hard-coded in the MSSqlServerChangeDatabaseHelper class and supplied to the DatabaseName  property editor using the PredefinedValues model option. To populate this list with database names that become available only at runtime (for example, the application reads the names from a configuration file or a database), consider the following options:

  3. For WinForms and ASP.NET WebForms, this XafApplication.ConnectionString-based implementation is designed for a simple scenario where the connection string doesn't store the user and password information. Otherwise, XAF removes the sensitive password information from the XafApplication.ConnectionString and you cannot rely on this API. In such scenarios, we recommend that you store the original connection string information in the CreateDefaultObjectSpaceProvider method of your XafApplication descendant (see the YourSolutionName.Wxx/WxxApplication.xx file) as demonstrated in the following example: XAF - How to generate a sequential number for a persistent object within a database transaction.

  4. For WinForms and ASP.NET WebForms, see the alternative solutions created by DevExpress MVPs Jose Columbie and Joche Ojeda here: XAF Blazor Change DB at runtime.

Files to Review

Common

WinForms

ASP.NET WebForms

Blazor Server

See Also

How to Implement a Multi-Tenant Application for Blazor and WinForms

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

About

How to change connection to the database at runtime from the login form in eXpressApp Framework

Topics

Resources

License

Stars

Watchers

Forks