以编程方式加载连接字符串

时间:2011-02-03 02:49:18

标签: nhibernate activerecord castle-activerecord

现在我必须在配置文件中指定活动记录/ nhibernate的连接字符串,如下所示:

<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>

然后我初始化活动记录:

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

这些连接字符串必须是数据库驱动的,所以我需要一种以编程方式设置它们的方法。我怎么能这样做?请记住,我连接到多个数据库,以防有所作为......