Files to look at:
- Program.cs (VB: Program.vb)
- WinApplicationEx.cs (VB: WinApplicationEx.vb)
- WebChangeDatabaseController.cs (VB: WebChangeDatabaseController.vb)
- WebApplication.cs (VB: WebApplicationEx.vb)
- WebApplicationEx.cs (VB: WebApplicationEx.vb)
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.
- 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.
- Copy code that creates predefined security users for each database from the RuntimeDbChooser.Module\DatabaseUpdate\Updater.xx file into YourSolutionName.Module/DatabaseUpdate/Updater.xx.
- Copy and include the RuntimeDbChooser.Module\BusinessObjects\CustomLogonParameters.xx file into the YourSolutionName.Module/BusinessObjects folder.
- 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.
- Copy and include the RuntimeDbChooser.Wxx\WxxApplicationEx.xx files into the YourSolutionName.Wxx project. Rename
RuntimeDbChooserWindowsFormsApplication
orRuntimeDbChooserAspNetApplication
orRuntimeDbChooserBlazorApplication
to yourWxxApplication
descendant's name from the WxxApplication.xx file. - Replace the line that instantiates your
WinApplication
descendant in the YourSolutionName.Win/Program.xx file with theCreateApplication
method call as shown in the RuntimeDbChooser.Win/Program.xx file. - In the Application Designer invoked for the YourSolutionName.Web/WebApplication.xx file, select the Authentication Standard component and set its
LogonParametersType
property toRuntimeDbChooser.Module.BusinessObjects.CustomLogonParametersForStandardAuthentication
. - Replace the line that instantiates your
BlazorApplication
in the YourSolutionName.Blazor.Server/Startup.cs file and set theAddAuthenticationStandard.Options.LogonParametersType
property toRuntimeDbChooser.Module.BusinessObjects.CustomLogonParametersForStandardAuthentication
.
- In this example, the available database names are hard-coded in the
MSSqlServerChangeDatabaseHelper
class and supplied to theDatabaseName
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:
- implement custom property editors and assign them to the
DatabaseName
property using the Model Editor: How to: Supply Predefined Values for the String Property Editor Dynamically. - customize logon parameters in the
XafApplication.LoggingOn
event handler (example).
- 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 theXafApplication.ConnectionString
and you cannot rely on this API. In such scenarios, we recommend you remember the original connection string information in theCreateDefaultObjectSpaceProvider
method of yourXafApplication
descendant (see inside the YourSolutionName.Wxx/WxxApplication.xx file) as demonstrated in the E2829 example. - You can find alternative solutions at https://youtu.be/o5t3Nb4zP7A (created by DevExpress MVPs Jose Columbie and Joche Ojeda).