为了添加其他原因。
我有一个VS2010解决方案/csproj,引用Enterprise Library 5.0.xxxxx。
然而,csproj被设置为"目标3.5框架"。因此,我需要将目标框架定位到4.0,或者使用Enterprise Library的3.5版本。
我成功地将目标框架更新为4.0,然后错误就消失了。
以下是您可以执行的操作,以确定代码编译的框架版本:
(Powershell迷你脚本)
[System.Reflection.Assembly]::LoadFrom("c:\somefolder\Any_Of_The_Practices.dll").ImageRuntimeVersion
如果你得到的是v2.0.50727版本,那么在Framework 4.0(或更高版本)下运行会出现问题。
编辑:
我也因为没有正确定义“DbProviderFactories”而遇到了这个错误。
我在以下网址找到了一个MySQL配置:
(我复制了它,以防该URL在将来失效)
(来自
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>
最后一点:
我曾经因为配置文件中没有与defaultDatabase值相同的ConnectionString而遇到了这个错误,也就是所谓的“傻瓜”错误。
如果您愿意,可以在创建数据库之前添加这个安全阀门代码。
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));
}
}
}