Log In
Home
Support
Demos
Documentation
Blogs
Training
Webinars
[Expand]General Information
[Expand]WinForms Controls
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Document Server
[Expand]Reporting
[Expand]Report Server
[Expand]Dashboard
[Collapse]eXpressApp Framework
 [Expand]Fundamentals
 [Expand]Getting Started
 [Collapse]Concepts
  [Expand]Application Solution Components
  [Collapse]Business Model Design
    Business Model Design Basics
    Built-in Business Classes for Most Popular Scenarios
   [Expand]Business Model Design with Entity Framework
   [Expand]Business Model Design with XPO
    Non-Persistent Objects
    PropertyChanged Event in Business Classes
   [Expand]Data Types Supported by built-in Editors
    Mapping Complex Types to the Database
   [Expand]Types Info Subsystem
    Connect an XAF Application to a Database Provider
    Data Annotations in Data Model
    Create and Update the Application's Database
  [Expand]Application Model
  [Expand]UI Construction
  [Expand]Extend Functionality
  [Expand]Data Manipulation and Business Logic
  [Expand]Security System
  [Expand]Localization
  [Expand]System Module
  [Expand]Extra Modules
  [Expand]Debugging and Error Handling
  [Expand]Filtering
  [Expand]Application Life Cycle
 [Expand]Design-Time Features
 [Expand]Functional Testing
 [Expand]Deployment
 [Expand]Task-Based Help
  Frequently Asked Questions
 [Expand]API Reference
[Expand]CodeRush
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation

Connect an XAF Application to a Database Provider

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 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, which serves as an Object-Relational Mapping layer, supports 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.

Expanded 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.

  • App.config - a WinForms application project's configuration file.
  • Web.config - an ASP.NET Web 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.

Important

It is not recommended to remove "Pooling=false;" from the connection string in XPO applications. This enables SQL Server Connection Pooling. When pooling is enabled, an error may occur when creating a new database. An SQL server cannot create a database using an existing connection from the pool, so XPO creates a separate connection with disabled pooling to create a database. As a result, an error occurs when the initial connection is not notified that the database is created.

Note that by default, XAF application projects are configured to connect to MS Access and the MS SQL server. However, if you connect to a different server, you need to perform two additional steps.

  1. Reference the required database provider connector assembly in the application projects. The complete list of the assemblies required for different database providers can be found in the Database Systems Supported by XPO document.
  2. Specify an appropriate XpoProvider parameter value in the connection string. The complete list of possible values for different database providers can be found in the How to create a correct connection string for XPO providers? Knowledge Base article.

When the connection string is commented or not specified in a configuration file, the connection string specified in the Application Designer is used by the application. This is provided by the following code, which is automatically added in each XAF solution.

WinForms
ASP.NET
Note

As you can see from the code above, the element named "ConnectionString" is used to obtain the connection string from the configuration file. So do not rename this element unless you change the corresponding string in the application code.

Expanded Approach 2. Specify the Connection String via the Application Designer

You can specify the connection string via the Application Designer's Connection section. Select the Connection item in this section to modify the ConnectionString property in the Properties window. Additionally, you can replace the SQL Connection with another available connection. For this purpose, drag the required connection from the Toolbox to the Connection section, and specify the ConnectionString property as required.

When you drag the required connection from the Toolbox to the Connection section, the required database provider assembly is automatically referenced. If the required connection component is not available, right-click the Toolbox to invoke the Choose Toolbox Items dialog, tick off the required connection component and click Ok.

Note

The connection string specified in the application configuration file overrides the connection string specified in the Application Designer. This is done to provide application administrators with an option to change the database connection without recompiling the application.

Expanded Get the Connection String to a Database

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.v17.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
ASP.NET

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 Application Designer or the configuration file. Note that a string returned by the GetConnectionString method contains the additional XpoProvider parameter. It identifies the database type to which a connection should be established. So, a string returned by the GetConnectionString method is not fully compatible with the standard connection string format. You can use this connection string in configuration files without modification, but the extra parameter has to be removed to use the string in the Application Designer.

How would you rate this topic?​​​​​​​