Error change defaultdatabase in forder ENT5

Topics: Data Access Application Block, Enterprise Library Core, General discussion
Mar 19, 2012 at 3:40 AM

Hi,

i have a web application 

 

in root folder i have a default web.config

<connectionStrings>

<add name="DataAccessQuickStart" connectionString="Data Source=x.x.x.x;Initial Catalog=BD1;Integrated Security=false;uid=sa;pwd=*****;" providerName="System.Data.SqlClient" /> </connectionStrings>

 <dataConfiguration defaultDatabase="DataAccessQuickStart" />

 

in folder /Aba i have a second web.config

<dataConfiguration defaultDatabase="frusanaba"/>
<connectionStrings>

 <add name="frusanaba" providerName="System.Data.SqlClient" connectionString="Data Source=x.x.x.x;Initial Catalog=BD2;Integrated Security=false;uid=sa;pwd=*****;Connect Timeout=200; pooling='true'; Max Pool Size=200" />

</connectionStrings>

 

in Enterprise library 2.0 works, the apps in folder /Aba using de second web.config for the default database,

but to migrate to  Enterprise library 5 not works, not assume the new default database.??

any idea? 

transk for the help.

best regard.

 

 

Mar 19, 2012 at 5:34 AM

It looks like Enterprise Library 5 will use the default database of whatever web.config is loaded first.  E.g. if /Aba/Test.aspx is loaded first then the default database will be set to the value in /Aba/web.config.  You are correct -- this changed sometime between version 2 and version 5.  I haven't looked in depth but I would guess it has to do with how Unity creates and loads the build plans for the various database objects.

So how to work around this?  Luckily, "all problems in computer science can be solved by another level of indirection".  

The way to get the database that you want is to go after it directly by name.  E.g. DatabaseFactory.CreateDatabase("frusanaba").   This should work fine.  However, I take it you still want to rely on configuration to determine the database instead of hard coding the database key in the code.  So you could create your own defaultDatabase appSettings value and then use that via a helper method.

E.g. in root web.config:

  <configSections>
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data" />
  </configSections>
  <dataConfiguration defaultDatabase="DataAccessQuickStart"/>
  <appSettings>
    <add key="defaultDatabase" value="DataAccessQuickStart"/>
  </appSettings>
  <connectionStrings>
    <add
			name="DataAccessQuickStart"
			providerName="System.Data.SqlClient"
			connectionString="server=(local)\SQLEXPRESS;database=EntLibQuickStarts;Integrated Security=true" />
  </connectionStrings>

Then in /Aba/web.config:

  <appSettings>
    <add key="defaultDatabase" value="frusanaba"/>
  </appSettings>
  <connectionStrings>
    <add name="frusanaba" providerName="System.Data.SqlClient" connectionString="Data Source=x.x.x.x;Initial Catalog=BD2;Integrated Security=false;uid=sa;pwd=*****;Connect Timeout=200; pooling='true'; Max Pool Size=200" />
  </connectionStrings>

And then create a helper method:

public Database CreateDefaultDatabase()
{
    return DatabaseFactory.CreateDatabase(ConfigurationManager.AppSettings["defaultDatabase"]);
}

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com