Load connection strings programmatically

941 views Asked by At

Right now I have to specify connection strings for active record/nhibernate in my config file like so:

<activerecord>
    <config type="Navtrak.Business.Schemas.CommonSchemas.Models.NavtrakOperations.NavtrakOperationsDatabase`1, CommonSchemas">
      <add key="connection.connection_string" value="connstring1" />
      <add key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
      <add key="dialect" value="NHibernate.Dialect.MsSql2005Dialect" />
      <add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
      <add key="show_sql" value="true" />
      <add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle" />
    </config>
    <config type="Navtrak.Business.Schemas.CommonSchemas.Models.Users.UsersDatabase`1, CommonSchemas">
      <add key="connection.connection_string" value="connstring2" />
      <add key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
      <add key="dialect" value="NHibernate.Dialect.MsSql2005Dialect" />
      <add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
      <add key="show_sql" value="true" />
      <add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle" />
    </config>
  </activerecord>

Then I initialize active record:

if (!ActiveRecordStarter.IsInitialized)
                ActiveRecordStarter.Initialize(typeof(SimpleModel).Assembly, ActiveRecordSectionHandler.Instance);

These connection strings must be database-driven so I need a way to programmatically set them. How can I do so? Keep in mind that I connect to multiple databases, in case that makes a difference...

1

There are 1 answers