Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
324 views
in Technique[技术] by (71.8m points)

c# - 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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:somefolderAny_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));
                }
            }
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...