Wednesday, August 8, 2012

The current build operation (build key Build Key[Microsoft.Practices.EnterpriseLibrary.Data.Database, null]) failed: The value can not be null or an empty string.


This error occurs when using the Microsoft EnterpriseLibrary Data Application block. Specifically, when attempting to create a database using the database factory.
Example:
Database db = DatabaseFactory.CreateDatabase();

The Error Message: The current build operation (build key Build Key[Microsoft.Practices.EnterpriseLibrary.Data.Database, null]) failed: The value can not be null or an empty string.

The Resolution:
EnterpriseLibrary Data Application block requires connection strings be configured. In this case, without specifying a connection string, the DatabaseFactory is attempting to use the default database.

In order to use a default database, you need to add the dataConfiguration element to your application configuration file.

The data configuration element contains a single attribute defaultDatabase which contains the name of the connection string to use as the default.

app.config file containing dataConfiguration element
Resources:Database Factory Class on MSDN



This is a simple example of a app.config file that contains the necessary elements:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data" />
  </configSections>
  <connectionStrings>
    <add name="MyDatabase" connectionString="Initial Catalog=MyApplicationDatabase; Data Source=MyApplicationDatabaseServer; User ID=MyUserName; Password=MyPassword" providerName="System.Data.SqlClient"/>
 </connectionStrings>
 <dataConfiguration defaultDatabase="MyDatabase"/>
</configuration>

No comments: