DatabaseFactory.CreateDatabase problem in DAAB 6.0

May 14, 2013 at 2: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 3:37 AM
Edited May 14, 2013 at 5: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 7: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 :)

Jul 18, 2014 at 4:39 PM

I know this post is over one year old. But I have a similar problem I thought to find an answer. Here is the problem.

I had an old project with all source and binaries. When I ran the application in Visual Studio 2012 Professional I received the following error. My old project was in Visual 2008.

Microsoft.Practice.EnterpriseLibrary.Data.DatabaseFactory does not contain a definition for SetDatabaseFactory

I have the following statement in my Gloabal.asax.cs file

protected void Application_Start(object sender, EventArgs e)

        Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory.SetDatabaseProviderFactory(new Microsoft.Practices.EnterpriseLibrary.Data.DatabaseProviderFactory(), false);


I would appreciate any assistant.

Jul 22, 2014 at 7:41 AM
The SetDatabaseProviderFactory method was introduced in Enterprise Library 6. It sounds like you are not referencing Enterprise Library 6 in your application. Are you referencing Enterprise Library 5?

Randy Levy
Enterprise Library support engineer
Support How-to
Feb 1, 2015 at 10:02 PM
I am having the same issues with DatabaseFactory. Where in the code or in the app.config do I add it?

Presently, I have it in one of my DAL class, compile it, only to tell me "InnerException: The static DatabaseFactory already has a database provider factory or custom methods set". So when I delete it out again, it keeps telling me the database provider factory is not set.

I cannot get help anywhere else. I posted here already. Please help!!