Files to look at:

Common

WinForms

ASP.NET WebForms

Blazor Server

How to change connection to the database at runtime from the logon form

Scenario

This example illustrates how to connect your application to another database after the application is already started. This can be required for a multi-tenant application where you need to associate a user or company with their own database of the same structure. You can choose a required database and user during the login procedure. The created databases will have the same structure, but can have a different predefined data set.

Implementation Steps

  1. Using the Solution Wizard, create a new XAF app named RuntimeDbChooser and that uses XPO for data access and the Security module with the Authentication = Standard and Integrated Mode options.
  2. Copy code that creates predefined security users for each database from the RuntimeDbChooser.Module\DatabaseUpdate\Updater.xx file into YourSolutionName.Module/DatabaseUpdate/Updater.xx.
  3. Copy and include the RuntimeDbChooser.Module\BusinessObjects\CustomLogonParameters.xx file into the YourSolutionName.Module/BusinessObjects folder.
  4. Copy and include the RuntimeDbChooser.Module\ChangeDatabaseActiveDirectoryAuthentication.xx file into the YourSolutionName.Module project. For more information on this API, see How to: Use Custom Logon Parameters and Authentication.
  5. Copy and include the RuntimeDbChooser.Wxx\WxxApplicationEx.xx files into the YourSolutionName.Wxx project. Rename 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. In the Application Designer invoked for the YourSolutionName.Web/WebApplication.xx file, 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 AddAuthenticationStandard.Options.LogonParametersType property to RuntimeDbChooser.Module.BusinessObjects.CustomLogonParametersForStandardAuthentication.

Important Notes

  1. In this example, the available 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 will be known only at runtime (for instance, read from a configuration file or a database), consider the following options:
  1. This XafApplication.ConnectionString-based implementation is designed for a simple scenario when no user and password information is stored in the connection string. Otherwise, the sensitive password information is automatically removed by XAF code from the XafApplication.ConnectionString and you cannot rely on this API. In such scenarios, we recommend you remember the original connection string information in the CreateDefaultObjectSpaceProvider method of your XafApplication descendant (see inside the YourSolutionName.Wxx/WxxApplication.xx file) as demonstrated in the E2829 example.
  2. You can find alternative solutions at https://youtu.be/o5t3Nb4zP7A (created by DevExpress MVPs Jose Columbie and Joche Ojeda).