Fluent Nhibernate Database configuration

Below is a quick code to connect sql database in Nhibernate using Fluent Nhibernate .

You can read more here for developing complete application.

static ISessionFactory _sessionFactory = null;
public static ISessionFactory CreateSessionFactory()
{
_sessionFactory = FluentNHibernate.Cfg.Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2005.ConnectionString(c => c
.Server(“localhost\\sqlexpress”)
.Database(“xxxx”)
.Username(“xxxxx”)
.Password(“xxxxxxxx”)))
.Mappings(m => m.FluentMappings.AddFromAssembly(System.Reflection.Assembly.Load(“FluentNhibernateMappingSample”)))
//.ExposeConfiguration(BuildSchema)
.BuildSessionFactory();
return _sessionFactory;}

private static void BuildSchema(Configuration config)
{
new SchemaExport(config).Create(true, true);
}

public static ISession GetNHBSession()
{
if (_sessionFactory == null)
{
_sessionFactory = CreateSessionFactory();

}
return _sessionFactory.OpenSession();
}

Advertisements

One thought on “Fluent Nhibernate Database configuration

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s