DatabaseFactory.CreateDatabase problem in DAAB 6.0

May 14, 2013 at 1:46 AM
Hi,

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:
  <connectionStrings>
    <add name="default" connectionString="Data Source=(local);database=MyAppDB;Integrated Security=True;" providerName="System.Data.SqlClient" />
  </connectionStrings>
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.

Hugs,
Serena
Editor
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
entlib.support@live.com
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 :)

Hugs,
Serena
Jul 18 at 3:39 PM
Greetings,

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);

        BuildContainer();

    }
I would appreciate any assistant.

Regards,
Alex
Editor
Jul 22 at 6: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
entlib.support@live.com
Enterprise Library support engineer
Support How-to