Monday, October 05, 2009

Fluent NHibernate – Connecting to SQLServer

Here is the code to create a ISessionFactory that can be used to work with a SqlServer database

ISessionFactory sessionFactory = Fluently.Configure()
.Database( MsSqlConfiguration.MsSql2008
.ConnectionString(m => m.Server(@".\SqlExpress")
.Database("Fluent_Test")
.TrustedConnection()))
.Mappings(m =>
m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly()))
.ExposeConfiguration((Configuration config) => new SchemaExport(config).Create(false,true))
.BuildSessionFactory();
return sessionFactory;

The code uses a local SqlExpress database called “Fluent_Test”. The mappings are stored in the same assembly as the method in which the above code is placed.

No comments: