Connect an XAF Application to a Database Provider(将 XAF 应用程序连接到数据库提供程序)
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.
当应用程序首次运行时,它的数据库会自动创建。在以下以调试模式运行期间,应用程序连接到同一数据库并根据需要进行更新。当应用程序以发布模式运行时,默认情况下不会更新数据库。有关详细信息,请参阅创建和更新应用程序的数据库主题。如果您需要重新创建数据库,只需将其从数据库服务器中删除或删除文件,它就会自动重新创建。默认情况下,XAF解决方案配置为使用本地系统上的MicrosoftSQLServer Express LocalDB实例,通过集成的安全性进行访问。将在该服务器上以您创建的解决方案的名称创建数据库。但是,XPO和EF Core作为对象关系映射层,支持十几个数据库管理系统。因此,您可以更改默认连接。为此,您需要指定所需的连接设置,并引用所需的数据库提供程序连接器程序集。本主题介绍了允许您将XAF应用程序连接到所需数据库提供程序的几种方法。
Approach 1. Specify the Connection String via the Application Projects’ Configuration Files(方法1.通过应用程序项目的配置文件指定连接字符串)
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.
WinForms应用程序项目的配置文件。 - Web.config - an ASP.NET Web Forms application project’s configuration file.
ASP.NET Web Forms应用程序项目的配置文件。 - appsettings.json - an ASP.NET Core Blazor application project’s configuration file.
ASP.NETCore Blazor应用程序项目的配置文件。
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.
默认情况下,这些配置文件包含连接字符串部分,其中包含许多注释的示例连接字符串。您可以取消注释并自定义现有连接字符串之一,或者通过以下方式指定新的连接字符串。请注意,默认情况下,XAF应用程序项目配置为连接到MicrosoftSQLServer Express LocalDB。
WinForms and Web Forms
XML
<connectionStrings>
<add name="ConnectionString" connectionString=
"Integrated Security=SSPI;Pooling=false;Data Source=(localdb)\mssqllocaldb;Initial Catalog=MySolution" />
</connectionStrings>
Blazor
JSON
{
"ConnectionStrings": {
"ConnectionString": "Integrated Security=SSPI;Pooling=false;Data Source=(localdb)\\mssqllocaldb;Initial Catalog=MySolution",
// ...
},
// ...
}
Important
- In XPO applications, the initial connection string declaration sets Pooling to false, thus overriding the default true value and disabling SQL Server Connection Pooling. We do not recommend deleting “Pooling = false;“ or setting Pooling to true. XPO uses a separate connection with disabled pooling to create a new database. If pooling is enabled, a SQL server attempts to create a database and tries to use an existing connection from the pool. This causes an error, because the connection is not notified that the database was created.
在XPO应用程序中,初始连接字符串声明将池化设置为false,从而覆盖默认真值并禁用SQL服务器连接池。我们不建议删除“Pooling=false;”或将池化设置为true。XPO使用禁用池化的单独连接来创建新数据库。如果启用了池化,SQL服务器会尝试创建数据库并尝试使用池中的现有连接。这会导致错误,因为连接没有被通知数据库已创建。- XAF requires Multiple Active Result Sets in EF Core-based applications connected to a Microsoft SQL Server database. We do not recommend that you remove MultipleActiveResultSets=True; from the connection string or set the MultipleActiveResultSets parameter to false. For other database management systems, we implement a mechanism internally that allows an application to correctly run without Multiple Active Result Sets.
XAF需要连接到MicrosoftSQLServer数据库的基于EF Core的应用程序中的多个活动结果集。我们不建议您从连接字符串中删除MultipleActiveResultSets=True;或将MultipleActiveResultSets参数设置为false。对于其他数据库管理系统,我们在内部实现了一种机制,允许应用程序在没有多个活动结果集的情况下正确运行。
If you connect to a different server, you need to perform two additional steps.
如果您连接到不同的服务器,则需要执行两个额外的步骤。
1.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:
将所需的数据库提供程序添加到应用程序项目中。可以在以下文档中找到不同数据库提供程序所需的程序集的完整列表:
* Database Systems Supported by XPO
* Database Providers (EF Core)
2.In XPO-based applications, 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 following section: How to create a correct connection string for XPO providers.
在基于XPO的应用程序中,在连接字符串中指定适当的XpoProvider参数值。不同数据库提供程序的可能值的完整列表可以在以下部分中找到:如何为XPO提供程序创建正确的连接字符串。
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.
在基于EF Core的应用程序中,按照以下主题中的说明修改XAF解决方案:将EF Core Connection从SQL服务器切换到不同的数据库提供程序。
In .NET Framework applications, when the connection string is commented or not specified in a configuration file, the connection string specified in the Application Designer is used. This is provided by the following code, which is automatically added in each XAF solution.
在. NET Framework应用程序中,当在配置文件中注释或未指定连接字符串时,将使用应用程序设计器中指定的连接字符串。这由以下代码提供,该代码会自动添加到每个XAF解决方案中。
WinForms
C#
public static void Main(string[] arguments) {
MySolutionWinApplication winApplication = new MySolutionWinApplication();
//...
if(ConfigurationManager.ConnectionStrings["ConnectionString"] != null) {
winApplication.ConnectionString = ConfigurationManager.
ConnectionStrings["ConnectionString"].ConnectionString;
}
winApplication.Setup();
winApplication.Start();
//...
}
ASP.NET Web Forms
C#
public class Global : System.Web.HttpApplication {
protected void Session_Start(object sender, EventArgs e) {
WebApplication.SetInstance(Session, new MySolutionWebApplication());
//...
if(ConfigurationManager.ConnectionStrings["ConnectionString"] != null) {
WebApplication.Instance.ConnectionString = ConfigurationManager.
ConnectionStrings["ConnectionString"].ConnectionString;
}
WebApplication.Instance.Setup();
WebApplication.Instance.Start();
}
//...
}
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.
从上面的代码可以看出,名为“ConnectionString”的元素用于从配置文件中获取连接字符串。因此,除非您更改应用程序代码中的相应字符串,否则不要重命名此元素。
Approach 2. Specify the Connection String via the Application Designer (.NET Framework Applications Only)(方法2.通过应用程序设计器指定连接字符串(仅限. NET Framework应用程序))
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.
您可以通过应用程序设计器的连接部分指定连接字符串。选择此部分中的连接项以修改属性窗口中的连接字符串属性。此外,您可以将SQL连接替换为另一个可用连接。为此,请将所需的连接从工具箱拖到连接部分,并根据需要指定连接字符串属性。
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.
应用程序配置文件中指定的连接字符串会覆盖应用程序设计器中指定的连接字符串。这样做是为了向应用程序管理员提供更改数据库连接的选项,而无需重新编译应用程序。
Get the Connection String to a Database (XPO-Based Approach)(获取到数据库的连接字符串(基于XPO的方法))
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.v24.1 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.
您可以通过XPO数据存储适配器类获取格式正确的连接字符串。这些类位于DevExpress. Xpo.v24.1程序集的DevExpress.Xpo.DB命名空间中。每个适配器类都包含GetConnectionString方法。使用此方法可以使用指定的参数获取到数据库的连接字符串。所有XPO适配器类都列在XPO支持的数据库系统主题中。
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.
以下代码片段演示了如何使用带有“pwd”密码的“usr”帐户连接到位于“DBSERVER”服务器上的“MyApp”MySql数据库。必须在应用程序项目中引用MySql.Data程序集。
WinForms
C#
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();
//...
}
ASP.NET Web Forms
C#
using DevExpress.Xpo;
using DevExpress.Xpo.DB;
//...
public class Global : System.Web.HttpApplication {
protected void Session_Start(object sender, EventArgs e) {
WebApplication.SetInstance(Session, new MySolutionWebApplication());
//...
WebApplication.Instance.ConnectionString =
MySqlConnectionProvider.GetConnectionString("DBSERVER", "user", "pwd", "MyApp");
WebApplication.Instance.Setup();
WebApplication.Instance.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 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.
如果要使用上述第一种或第二种方法指定连接字符串,请在调试器中查看GetConnectionString方法返回的值,并将其复制到Application Designer或配置文件中。请注意,GetConnectionString方法返回的字符串包含附加的XpoProvider参数。它标识应该建立连接的数据库类型。因此,GetConnectionString方法返回的字符串与标准连接字符串格式不完全兼容。您可以在配置文件中使用此连接字符串而无需修改,但必须删除额外的参数才能在Application Designer中使用该字符串。