web.config中的NHibernate配置 - 使用现有的连接字符串

Mik*_*ole 10 nhibernate nhibernate-configuration

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

Sly*_*Sly 17

您可以在NHibernate配置中使用connection.connection_string_name元素.看看这里.然后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>
Run Code Online (Sandbox Code Playgroud)

使用流畅的配置,您可以执行以下操作

ConnectionString(c=>c.FromConnectionStringWithKey("YourConnStrName"))
Run Code Online (Sandbox Code Playgroud)

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

var cfg = new Configuration();
cfg.DataBaseIntegration(db =>
{
    db.ConnectionStringName = "default";             
});
Run Code Online (Sandbox Code Playgroud)