Configuring IBM DB2 (Generic) Database Provider

Topics: Data Access Application Block
Jun 12, 2013 at 3:19 PM
Edited Jun 12, 2013 at 3:21 PM
I use the following configuration in Enterprise Library 5.0 .NET 4.0 code and it works fine:

type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data" 
requirePermission="true" />

    Microsoft.Practices.EnterpriseLibrary.Data" name="IBM.Data.DB2.iSeries" />

name="CONNECTION" connectionString="DataSource=DB2DATABASE;UserID=WHINYBABY;Password=WAHWAHWAH;DataCompression=True;" 
providerName="IBM.Data.DB2.iSeries" />

On Enterprise Library 6.0 in .NET 4.5 it fails with:

Database provider factory not set for the static DatabaseFactory. Set a provider factory invoking the DatabaseFactory.SetProviderFactory method or by specifying custom mappings by calling the DatabaseFactory.SetDatabases method.

I suppose this means that the configuration is failing but the question is why? Is there a machine config entry that matters here? (There is nothing else strange about my environment - I'm just trying to talk to an AS400 DB2 database which I can hit thru the dedicated driver with no problems. Just want to use the DAAB (like I was able to in 5.0.)

Any pointers?
Jun 12, 2013 at 3:35 PM
The bootstrapping of the block has changed in version 6.

To load the configuration you can use the following:
IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();
DatabaseFactory.SetDatabaseProviderFactory(new DatabaseProviderFactory(configurationSource));
The IConfigurationSource is not strictly required if the configuration resides directly in the app/web config but has the added benefit of working if the configuration is externalized and referenced via a FileConfigurationSource.

To use the default .NET configuration file (app/web config) then you could just use:
DatabaseFactory.SetDatabaseProviderFactory(new DatabaseProviderFactory());
Randy Levy
Enterprise Library support engineer
Support How-to
Jun 12, 2013 at 4:08 PM
OK - so I tried that and whether I use the IConfigurationSource call or not, or whether I leave in or comment out the dataConfiguration section and providerMappings I get every time:

Outer Error:
The connection string for the database 'CONNECTION' does not exist or does not have a valid provider.
Inner Error:
The 'DbProviderFactories' section can only appear once per config file.

Why was this changed? (It was too easy to get working?)

Are you saying that I can no longer configure a generic database thru the configuration file alone? I must use code to configure providers?

Is there a way to examine the defined providers? (I suspect I need to do this because apparently the runtime is getting one or more providers defined from somewhere.)

Jun 12, 2013 at 4:23 PM
Can you check machine.config for any rogue DbProviderFactories sections as per DbProviderFactories section can only appear once per config file?

Randy Levy
Enterprise Library support engineer
Support How-to
Marked as answer by randylevy on 10/7/2013 at 11:16 PM
Jun 12, 2013 at 6:43 PM
You-reeka! Thank you so much (not much hair left so not looking forward to wasting more on this issue.) Why the extra (unused?) element? I removed it, rebooted and everything worked (with the call to SetDatabaseProviderFactory().) Now I just need to figure out how to call SetDatabaseProviderFactory() only once!