DatabaseFactory.CreateDatabase problem in DAAB 6.0

May 14, 2013 at 1:46 AM

I migrated a simple application from DAAB 5.0 to 6.0 and after migration, the code throws an exception when calling
Database db = DatabaseFactory.CreateDatabase("default");
The exception message was:

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.

My config file setting is:
    <add name="default" connectionString="Data Source=(local);database=MyAppDB;Integrated Security=True;" providerName="System.Data.SqlClient" />
I tried to NuGet the EntLib libraries again. I have tried Cleaning the solution and manually deleted any traces of 5.0 dlls in my bin folder. I have tried creating a new project to test. The same error exist.

The error does not occur when I revert back to EntLib 5.

May 14, 2013 at 2:37 AM
Edited May 14, 2013 at 4:04 AM
With Enterprise Library 6 to configure the blocks using XML configuration you can use factory methods. For the Database Application Block you can use the SetDatabaseProviderFactory method:
DatabaseFactory.SetDatabaseProviderFactory(new DatabaseProviderFactory());

This needs to happen once before using the block and lets you use the static CreateDatabase() call.

Alternately, you could replace DatabaseFactory.CreateDatabase with:
Database db = new DatabaseProviderFactory().Create("default");
Although you probably wouldn't want to create a new DataProviderFactory for each invocation and instead keep a reference to the DatabaseProviderFactory instance and use that to create a database.

Randy Levy
Enterprise Library support engineer
Support How-to
Marked as answer by randylevy on 2/23/2014 at 9:40 PM
May 21, 2013 at 6:38 AM
Hi Randy! :)

I have included the following line in a static constructor in my class:
DatabaseFactory.SetDatabaseProviderFactory(new DatabaseProviderFactory());
... and everything works fine as usual.

Thank You :)