Enterprise Library 6: InvalidOperationException - 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
Last week I finally had some time to have a look at Enterprise Library version 6. The Microsoft Patterns and Practices team decided to remove the dependency on Unity, so creating an EntLib object should be done using one of the available factory classes.
In this case I tried to created an instance of a Database object:
In this case I tried to created an instance of a Database object:
Database db = DatabaseFactory.CreateDatabase("Northwind");
Life is never so simple, so it failed with the following exception message: “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.”With Enterprise Library 6 we first have to specify how the application block should load it configuration information. For the Database Application Block you can use the SetDatabaseProviderFactory method:
DatabaseFactory.SetDatabaseProviderFactory(new DatabaseProviderFactory());
Probably even better is to avoid the static DatabaseFactory at all and use the DatabaseProviderFactory class directly:
DatabaseProviderFactory factory = new DatabaseProviderFactory(); Database db = factory.Create("Northwind");