Activation error occurred while trying to get instance of type Database, key "CoreTechnical"

Topics: Data Access Application Block
Mar 13, 2013 at 8:16 PM
I'm getting the error on this line of code:

Database SqlDb = DatabaseFactory.CreateDatabase("CoreTechnical");

The InnerException is:
Resolution of the dependency failed, type = "Microsoft.Practices.EnterpriseLibrary.Data.Database", name = "CoreTechnical".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The type Database cannot be constructed. You must configure the container to supply this value.

Here is how the web.config looks:

<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data,Version=5.0.414.0,Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</configSections>
<dataConfiguration defaultDatabase="EquityOps">
<providerMappings>
  <add databaseType="Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase, Microsoft.Practices.EnterpriseLibrary.Data,Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</providerMappings>
</dataConfiguration>
<connectionStrings>
<add name="EquityOps" connectionString="Data Source=serverA;Initial Catalog=EquityOps;Integrated Security=true;" providerName="System.Data.SqlClient"/>
<add name="CoreTechnical" connectionString="Data Source=serverA;Initial Catalog=CoreTechnical;Integrated Security=true;" providerName="System.Data.SqlClient"/>
</connectionStrings>

I have GACed the Data, Common, and Unity dlls. In the actual web.config, I've given the server name.

Thanks for the help.
Mar 14, 2013 at 6:09 AM
Edited May 1, 2013 at 9:37 PM
If you have deployed in the GAC then you will need a binding redirect:
<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <qualifyAssembly partialName="Microsoft.Practices.EnterpriseLibrary.Data" fullName="Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
  </assemblyBinding>
</runtime>
~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Mar 14, 2013 at 11:47 AM
Thanks Randy. Where do I add that in the web.config?
Mar 14, 2013 at 1:57 PM
Edited Mar 14, 2013 at 2:34 PM
I'm getting this error: required attribute 'name' not found. I've added the runtime between the end tag of appSettings and start tag of system.web.
Mar 14, 2013 at 3:55 PM
Yes, the binding redirect should go in the web.config.

The error wouldn't be generated by the binding redirect. I think the providerMappings is missing a name attribute. However, unless you are a custom DbProvider (e.g. ODP.NET) you shouldn't need the providerMappings configuration.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Mar 14, 2013 at 4:05 PM
I got that error after adding the runtime element.
Mar 14, 2013 at 4:33 PM
Can you post the entire web.config? What line does the exception say the error occurs on?

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to