如何管理单独项目的 NHibernate 和 Asp.NET MVC ConnectionStrings

发布于 2024-09-14 12:59:56 字数 699 浏览 4 评论 0 原文

我想就管理以下事项的最佳方式征求意见。我有一个 MVC 应用程序项目,以及一个 DomainModel 项目。 NHibernate 映射和 hibernate.cfg.xml 位于 MVC 应用程序中引用的 DomainModel 项目中。这通常可以正常工作,因为我所有的数据访问都将通过 NHibernate 连接。

问题是我正在使用 Sql Membership 提供程序,因此需要直接从 Asp.net MVC 中访问连接字符串。

问题 1

有人以前遇到过这个问题吗?最干净的解决方案是什么?我想确保只有一个地方可以存储连接字符串。

问题 2

我如何使用两个单独的连接配置 NHibernate,以便使用代码进行调试和发布

#if DEBUG
 connection = configuration.Properties[/*DEBUGconnectionstring-hibernate.cfg.xml*/]
#else
 connection = configuration.Properties[/*RELEASEconnectionstring-hibernate.cfg.xml*/]
#endif 

目前唯一有效的属性是 connection.connection_string。

你会如何解决这个问题?

谢谢。

I would like to get opinion on the best way to manage the following please. I have an MVC application project, as well as a DomainModel project. The NHibernate mappings and hibernate.cfg.xml are in the DomainModel project which is referenced in the MVC application. This would normally work fine as all my dataaccess would go through the NHibernate connection.

The problem is that I am using the Sql Membership provider so need access to a connection string from directly within Asp.net MVC.

Question 1

Has anyone had this issue before and what would be the cleanest solution? I want to make sure I only have one place that stores the connectionstring.

Question 2

How would I configure NHibernate with two seperate connections, for Debug and Release with the code

#if DEBUG
 connection = configuration.Properties[/*DEBUGconnectionstring-hibernate.cfg.xml*/]
#else
 connection = configuration.Properties[/*RELEASEconnectionstring-hibernate.cfg.xml*/]
#endif 

Currently the only valid property is connection.connection_string.

How would you get around this?

Thanks.

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

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぃ双果 2024-09-21 12:59:56

也许将你的 nhibernate 配置信息移动到你的项目配置文件(app.config 或 web.config)可以解决你的问题。例如,我在一个解决方案中有两个项目。其中一种是WCF服务,另一种是win服务。我有以下每个项目配置文件的连接信息。

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
      <property name="dialect">NHibernate.Dialect.Oracle10gDialect</property>
      <property name="default_schema">DUMMY</property>
      <property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property>
      <property name="connection.connection_string">Data Source=SRV_OLDEV;User Id=USRdummy;Password=USRdummy;</property>

      <property name="proxyfactory.factory_class">
        NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu
      </property>
      <property name="show_sql">true</property>
    </session-factory>
  </hibernate-configuration>

Maybe moving your nhibernate config information to your project config files (app.config or web.config) solves your problem. For example, I have two projects in one solution. One of them is WCF service and other one is win service. I have following connection information seperatly each project config files.

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
      <property name="dialect">NHibernate.Dialect.Oracle10gDialect</property>
      <property name="default_schema">DUMMY</property>
      <property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property>
      <property name="connection.connection_string">Data Source=SRV_OLDEV;User Id=USRdummy;Password=USRdummy;</property>

      <property name="proxyfactory.factory_class">
        NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu
      </property>
      <property name="show_sql">true</property>
    </session-factory>
  </hibernate-configuration>
╄→承喏 2024-09-21 12:59:56

我使用 FNH 在代码中设置了与 NH 的连接。这可能不适合您,但如果是的话,这个问题就会消失。

I set my connections to NH in code, using FNH. This may not be an option for you, but if it is, this problem goes away.

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