Connect an XAF Application to a Database Provider
- 4 minutes to read
When an application runs for the first time, its database is created automatically. During the following runs in a debug mode, the application connects to the same database and updates it as required. When the application runs in a release mode, the database is not updated by default. For details, refer to the Create and Update the Application’s Database topic. If you ever need to have your database recreated, just drop it from the database server or remove the file, and it will be recreated automatically. By default, an XAF solution is configured to use an instance of the Microsoft SQL Server Express LocalDB on the local system, accessed via the integrated security. A database will be created on that server under the name of the solution you created. However, XPO and EF Core, which serve as an Object-Relational Mapping layer, support over a dozen database management systems. So you can change the default connection. To do this, you need to specify the required connection setting, and reference the required database provider connector assembly. This topic describes several approaches that allow you to connect an XAF application to the required database provider.
Approach 1. Specify the Connection String via the Application Projects’ Configuration Files
When you need to access the connection string in a compiled application, specify it via the application projects’ configuration files.
- appsettings.json - an ASP.NET Core Blazor application project’s configuration file.
- App.config - a WinForms application project’s configuration file.
By default, these configuration files contain the connectionStrings section, which has a number of commented sample connection strings. You can uncomment and customize one of the existing connection strings, or specify a new connection string in the following way. Note that by default, XAF application projects are configured to connect to Microsoft SQL Server Express LocalDB.
{
"ConnectionStrings": {
"ConnectionString": "Integrated Security=SSPI;Pooling=false;Data Source=(localdb)\\mssqllocaldb;Initial Catalog=MySolution",
// ...
},
// ...
}
Important
- XPO-specific note
- In XPO applications, the connection string initially sets
Poolingtofalse, which disables SQL Server Connection Pooling. Do not removePooling = false;or set it totrue. - EF Core-specific note
XAF requires Multiple Active Result Sets (MARS) for EF Core applications connected to Microsoft SQL Server. Do not remove
MultipleActiveResultSets=true;and do not set it tofalsein the connection string. For databases that do not support MARS, XAF has an internal mechanism to function without MARS. To enable it, includePersist Security Info=Truein the connection string.
If you connect to a different server, you need to perform the following steps:
- Add the required database provider to the application projects. The complete list of the assemblies required for different database providers can be found in the following documents:
- Specify an appropriate provider in the connection string:
- In XPO-based applications, use the
XpoProviderparameter. See the list of available parameter values. - In EF Core-based applications, use the
EFCoreProviderparameter.
- In XPO-based applications, use the
- In EF Core-based applications, modify an XAF solution as described in the following topic: Switch EF Core Connection from SQL Server to a Different Database Provider.
Get the Connection String to a Database (XPO-Based Approach)
You can obtain properly formatted connection strings via XPO data store adapter classes. These classes are located in the DevExpress.Xpo.DB namespace of the DevExpress.Xpo.v25.2 assembly. Each adapter class contains the GetConnectionString method. Use this method to obtain a connection string to the database using specified parameters. All XPO adapter classes are listed in the Database Systems Supported by XPO topic.
The following code snippet demonstrates how to connect to a “MyApp” MySql database located on the “DBSERVER” server using the “usr” account with the “pwd” password. The MySql.Data assembly must be referenced in the application project.
WinForms
using DevExpress.Xpo;
using DevExpress.Xpo.DB;
//...
public static void Main(string[] arguments) {
MySolutionWinApplication winApplication = new MySolutionWinApplication();
//...
winApplication.ConnectionString =
MySqlConnectionProvider.GetConnectionString("DBSERVER", "usr", "pwd", "MyApp");
winApplication.Setup();
winApplication.Start();
//...
}
If you want to specify the connection string using the first or the second approach described above, look at the value returned by the GetConnectionString method in the debugger, and copy it to the configuration file.