以编程方式加载连接字符串
现在我必须在我的配置文件中指定活动记录/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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用InPlaceConfigurationSource。一些示例:
Use
InPlaceConfigurationSource
. Some examples: