【问题标题】:NHibernate Configuration in web.config - use existing connectionstringweb.config 中的 NHibernate 配置 - 使用现有的连接字符串
【发布时间】:2009-11-17 15:00:37
【问题描述】:

我在 web.config 文件中成功设置了 NHibernate 配置。但是,我也在使用 ASP.NET Membership,它需要在 connectionStrings 元素中定义一个连接字符串。有没有办法让我的 NHibernate 配置使用这个值,这样我就不需要定义连接字符串两次?

【问题讨论】:

    标签: nhibernate nhibernate-configuration


    【解决方案1】:

    您可以在 NHibernate 配置中使用 connection.connection_string_name 元素。看看here。然后 NHibernate 将从 web.config 文件中按名称获取连接字符串

    需要在配置中使用connection.connection_string_name属性

    <connectionStrings>
        <add name="default" connectionString="server=(local);etc." />
    </connectionStrings>
    
    <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
        <session-factory>
            <property name="connection.connection_string_name">default</property>
        </session-factory>
    </hibernate-configuration>
    

    通过流畅的配置,您可以执行以下操作

    ConnectionString(c=>c.FromConnectionStringWithKey("YourConnStrName"))
    

    使用 NHibernate 配置 API,您可以执行以下操作:

    var cfg = new Configuration();
    cfg.DataBaseIntegration(db =>
    {
        db.ConnectionStringName = "default";             
    });
    

    【讨论】:

    【解决方案2】:

    只是为了添加到 sly 的答案中,您可以像这样使用 FluentNHibernate 来做到这一点(在您的流利配置中):

    .ConnectionString(c=>c.FromConnectionStringWithKey("con_development"))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多