I have my NHibernate configuration successfully set up in my web.config file. However, I am also using ASP.NET Membership which requires a connectionstring to be defined in the connectionStrings element. Is there a way I can make my NHibernate configuration use this value so I don't need to define the connection string twice?
            Asked
            
        
        
            Active
            
        
            Viewed 5,909 times
        
    2 Answers
17
            You can use connection.connection_string_name element in the NHibernate configuration. Have a look here. Then NHibernate will get connection string by name from web.config file
You need to use the connection.connection_string_name attribute in the configuration:
<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>
With fluent configuration you can do the following
ConnectionString(c=>c.FromConnectionStringWithKey("YourConnStrName"))
With NHibernate configuration API you can do the following:
var cfg = new Configuration();
cfg.DataBaseIntegration(db =>
{
    db.ConnectionStringName = "default";             
});
        Sly
        
- 15,046
 - 12
 - 60
 - 89
 
- 
                    I wish you quoted the solution to give me hints as the page is not found anymore :( – Michal Ciechan Apr 21 '10 at 11:40
 - 
                    http://community.devpinoy.org/blogs/bonskijr/archive/2007/04/08/using-connectionstring-section-in-nhibernate.aspx – Sly Apr 21 '10 at 12:37
 
1
            
            
        Just to add to sly's answer, you can do this using FluentNHibernate like this (in your fluent config):
.ConnectionString(c=>c.FromConnectionStringWithKey("con_development"))
        UpTheCreek
        
- 31,444
 - 34
 - 152
 - 221