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

发布于 2024-10-16 01:14:08 字数 1746 浏览 0 评论 0原文

现在我必须在我的配置文件中指定活动记录/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);

这些连接字符串必须是数据库驱动的,因此我需要一种以编程方式设置它们的方法。我怎样才能这样做呢?请记住,我连接到多个数据库,以防产生影响......

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文