Giter VIP home page Giter VIP logo

xaf_how-to-change-connection-to-the-database-at-runtime-e1344's Introduction

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.

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

xaf_how-to-change-connection-to-the-database-at-runtime-e1344's People

Contributors

andreykozhevnikov avatar crimp avatar kill4kan avatar dennis-garavsky avatar anastasiakiseleva avatar tikhomirovp avatar devexpressexamplebot avatar trokkin avatar natakazakova avatar dependabot[bot] avatar maksimkarpenko avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.