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.
Alternative solutions: How to Implement a Multi-Tenant Application for Blazor and WinForms
-
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.
-
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.
-
Copy and include the RuntimeDbChooser.Module\BusinessObjects\CustomLogonParameters.xx file into the YourSolutionName.Module\BusinessObjects folder.
-
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.
-
Copy and include the RuntimeDbChooser.Wxx\WxxApplicationEx.xx files into the YourSolutionName.Wxx project. Rename the
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. -
Open the YourSolutionName.Web/WebApplication.xx file in the Application Designer. 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 theAddPasswordAuthentication.Options.LogonParametersType
property toRuntimeDbChooser.Module.BusinessObjects.CustomLogonParametersForStandardAuthentication
. -
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.
-
In this example, XAF Blazor applications load the available database names from the appsettings.json file.
-
In WinForms and ASP.NET WebForms applications, the 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 become available only at runtime (for example, the application reads the names from a configuration file or a database), consider the following options:- Implement custom property editors and assign them to the
DatabaseName
property in the Model Editor. For additional information, refer to the following article: How to: Supply Predefined Values for the String Property Editor Dynamically. - Customize login parameters in the
XafApplication.LoggingOn
event handler. For additional information, refer to the following example: Dynamic Database Name XAF Blazor.
- Implement custom property editors and assign them to the
-
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 theXafApplication.ConnectionString
and you cannot rely on this API. In such scenarios, we recommend that you store the original connection string information in theCreateDefaultObjectSpaceProvider
method of yourXafApplication
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. -
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.
Common
- Updater.cs (EFCore: Updater.cs)
- CustomLogonParameters.cs (EFCore: CustomLogonParameters.cs)
WinForms
ASP.NET WebForms
Blazor Server
- Startup.cs (XPO: Startup.cs)
- BlazorApplication.cs (XPO: BlazorApplication.cs)
- (XPO: XpoDataStoreProviderAccessor.cs)
- ConnectionStringProvider.cs (XPO: ConnectionStringProvider.cs)
- ConnectionStringHelper.cs (XPO: ConnectionStringHelper.cs)
- (EFCore DbContext.cs)
How to Implement a Multi-Tenant Application for Blazor and WinForms
(you will be redirected to DevExpress.com to submit your response)