1

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.

amedina
  • 2,838
  • 3
  • 19
  • 40
  • Have you tried reading documentation? Specifically, links 1 and 2 on [this page] (https://www.ibm.com/support/knowledgecenter/SSEPGG_11.1.0/com.ibm.swg.im.dbclient.adonet.doc/doc/c0054751.html) show you the correct provider name and the connection string properties, respectively. – mustaccio Jan 15 '19 at 11:49
  • @mustaccio Those articles are about .NET Framework, not about .NET Core. Anyway, I tried the provider name they suggest (`IBM.Data.DB2`), and it doesn't work anyway. – amedina Jan 15 '19 at 12:57

1 Answers1

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.

amedina
  • 2,838
  • 3
  • 19
  • 40