Activation error occured while trying to get instance of type Database, key "" <-- blank
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));
}
}
}
This is due to dll mismatch between the GAC dlls(Dlls in the assembly) and the dlls referred by the application.
Remove the dlls from the assembly and try run.
- Start --> Run
- Type Assembly
- Remove the dll for which activation error is happening.
- Reset the IIS
Hopefully it will solve your problem.
i was referencing the wrong .dll copy. it works now.
I have same problem and i solve it by change EntLib from 5.0 to 3.1
As class libraries can have app.config file. It is really not helpful when you are using EntLib 5.0 & want to access connection string in the class libarry. Hence you need to add the configuration above to all the web.config files that you are using in your solution. For example, if you have UI layer, WCF layer between Business(class library) and UI layer and the Business layer is in between WCF service and the DAL (class Library).
Here you will have one web.config file in each UI layer & the WCF serveice Layer, so these two web.config file should conation the above mentioned settings.
Make sure your server has the MySQL .net connector installed. I was getting the same error until I installed the connector. http://www.mysql.com/products/connector/
I was able to fix this error in my DLL project by specifying the name of the connection string explicitly in the CreateDatabase call, like this:
Database db = DatabaseFactory.CreateDatabase("DBTEST");
instead of
Database db = DatabaseFactory.CreateDatabase();
精彩评论