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