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

守給你的承諾、 提交于 2019-12-13 03:21:02

问题


I'm using this Microsoft.EnterpriseLibrary port for .Net Core. It requires a configuration file app.config with the connection string specified in it. I tried using the same connection string I use in another working project, but it doesn't work here.

How can I specify a DB2 connection string for ASP.NET Core 2.1?

I this is what I have tried:

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

But when I execute this:

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

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

It throws me this exception:

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.

I think the provider name is incorrect, but I can't find any working example on the web.


回答1:


Ok, I solved it. It turned out that I couldn't find any provider for IBM.Data.DB2.Core, so I configured my app.config file like this:

<?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>

Exploring the IBM assembly, I discovered there was a DbProviderFactory implementation class called DB2Factory. That class can be used instead a provider name if the app.config file is configured with a system.data section as shown above.



来源:https://stackoverflow.com/questions/54195417/ibm-db2-core-connection-string-for-asp-net-core-2-1-and-microsoft-enterprise-li

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!