IBM DB2 Core:ASP.NET Core 2.1 和 Microsoft Enterprise Libraries 的连接字符串

IBM DB2 Core: Connection string for ASP.NET Core 2.1 and Microsoft Enterprise Libraries

我正在为 .Net Core 使用 this Microsoft.EnterpriseLibrary 端口。它需要一个配置文件 app.config,其中指定了连接字符串。我尝试使用我在另一个工作项目中使用的相同连接字符串,但它在这里不起作用。

如何为 ASP.NET Core 2.1 指定 DB2 连接字符串?

我这是我试过的:

<connectionStrings>
    <add name="Development" connectionString="server=MY.SERVER.COM:446;database=DBXX;user id=USERXX;password=PASSWORDXX;" providerName="IBM.Data.DB2.Core"/>
</connectionStrings>

但是当我执行这个时:

DatabaseFactory.SetDatabaseProviderFactory(
    new DatabaseProviderFactory(
        new SystemConfigurationSource(false).GetSection
    ), 
    false
);

var db = DatabaseFactory.CreateDatabase("Development");

它抛出这个异常:

Exception has occurred: CLR/System.InvalidOperationException
An exception of type 'System.InvalidOperationException' occurred in Microsoft.Practices.EnterpriseLibrary.Data.dll but was not handled in user code: 'The connection string for the database 'Development' does not exist or does not have a valid provider.'
 Inner exceptions found, see $exception in variables window for more details.
 Innermost exception     System.Configuration.ConfigurationErrorsException : The requested database Development does not have a valid ADO.NET provider name set in the connection string.

我认为提供商名称不正确,但我在网上找不到任何可用的示例。

好的,我解决了。原来我找不到 IBM.Data.DB2.Core 的任何提供程序,所以我这样配置我的 app.config 文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

    <configSections>
        <section name="system.data" type="System.Data.Common.DbProviderFactoriesConfigurationHandler, Microsoft.Practices.EnterpriseLibrary.Data, Version=6.0.0.0, Culture=neutral, PublicKeyToken=null" requirePermission="true" />
        <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=6.0.0.0, Culture=neutral, PublicKeyToken=null" requirePermission="true" />
    </configSections>

    <dataConfiguration defaultDatabase="Development" />

    <connectionStrings>
        <add name="Development" connectionString="server=MY.SERVER.COM:446;database=DBXX;user id=USERXX;password=PASSWORDXX;" providerName="IBM.Data.DB2.Core"/>
    </connectionStrings>

    <system.data>
        <DbProviderFactories>
            <remove invariant="IBM.Data.DB2.Core" />
            <add name="DB2 Data Provider" invariant="IBM.Data.DB2.Core" description=".Net Framework Data Provider for DB2" type="IBM.Data.DB2.Core.DB2Factory, IBM.Data.DB2.Core" />
        </DbProviderFactories>
    </system.data>

</configuration>

探索 IBM 程序集,我发现有一个名为 DB2FactoryDbProviderFactory 实现 class。如果 app.config 文件配置了 system.data 部分,则可以使用 class 代替提供者名称,如上所示。