I'm trying out the Enterprise Library 5.0 and was doing some unit-tests on my BL, do I need to have a app.config on the DL or on the Test project?
note: I already have the configuration settings on my web.config on my web project.
how I use the DAAB:
private static Database db = DatabaseFactory.CreateDatabase();
db.ExecuteNonQuery("spInsertSalesman", salesman.Fullname);
my app.config on DL:
<configuration>
<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" />
</configSections>
<dataConfiguration defaultDatabase="DBTEST" />
<connectionStrings>
<add name="DBTEST" connectionString="Data Source=[dbsource];Initial Catalog=[dbname];User Id=sa;Password=password;"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
To add to other reasons.
I had a VS2010 solution/csproj that was referencing Enterprise Library 5.0.xxxxx.
However, the csproj was set to "Target 3.5 Framework". Thus I either needed to target the 4.0 framework OR go "down" to the 3.5 version of the Enterprise Library.
I was able to update my target framework to 4.0, and then the errors went away.
Here is what you can do to figure out what framework version the code was compiled for: (Powershell mini script)
[System.Reflection.Assembly]::LoadFrom("c:\somefolder\Any_Of_The_Practices.dll").ImageRuntimeVersion
If you get v2.0.50727, then you'll have trouble running under Framework 4.0 (or above).
EDIT:
I also got this error by not having the correct "DbProviderFactories" defined. I am posting a MySql configuration that I found at this URL: (I am copying it, in case the URL dies in the future)
(From http://searchcode.com/codesearch/view/14385662 )
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true"/>
</configSections>
<system.data>
<DbProviderFactories>
<add name="EntLibContrib.Data.MySql"
invariant="EntLibContrib.Data.MySql"
description="EntLibContrib Data MySql Provider"
type="EntLibContrib.Data.MySql.MySqlDatabase, EntLibContrib.Data.MySql, Version=5.0.505.0, Culture=neutral, PublicKeyToken=null" />
</DbProviderFactories>
</system.data>
<dataConfiguration defaultDatabase="Default Connection String">
<providerMappings>
<add databaseType="EntLibContrib.Data.MySql.MySqlDatabase, EntLibContrib.Data.MySql, Version=5.0.505.0, Culture=neutral, PublicKeyToken=null"
name="EntLibContrib.Data.MySql"/>
</providerMappings>
</dataConfiguration>
<connectionStrings>
<add name="Default Connection String"
connectionString="database=northwind;uid=root;pwd=admin"
providerName="EntLibContrib.Data.MySql"/>
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
Lastly:
Once I got this error because I did not have a ConnectionString (in the config file) with the same name as the defaultDatabase value. Aka, the simple "numb skull" error.
You can put this safety valve code in (right before you create the database) if you'd like.
DatabaseSettings dataConfig = (DatabaseSettings)ConfigurationManager.GetSection("dataConfiguration");
string configDefaultDatabase = string.Empty;
if (null != dataConfig)
{
configDefaultDatabase = dataConfig.DefaultDatabase;
if (!String.IsNullOrEmpty(configDefaultDatabase))
{
ConnectionStringSettingsCollection connections = ConfigurationManager.ConnectionStrings;
if (null == connections[configDefaultDatabase])
{
throw new ArgumentOutOfRangeException(
string.Format(
"Your dataConfiguration (DefaultDatabase) does not match any of your connection strings. DefaultDatabase='{0}'.",
configDefaultDatabase));
}
}
}