FirmTracker-Server/nHIbernate/SessionFactory.cs

42 lines
1.4 KiB
C#

using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Tool.hbm2ddl;
namespace FirmTracker_Server.nHibernate
{
public static class SessionFactory
{
private static ISessionFactory _factory;
public static NHibernate.ISession OpenSession()
{
return _factory.OpenSession();
}
public static void Init(string connectionString)
{
_factory = BuildSessionFactory(connectionString);
}
private static ISessionFactory BuildSessionFactory(string connectionString)
{
return Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2012
.ConnectionString(c => c.Is(connectionString))
.ShowSql())
.Mappings(m =>
{
m.FluentMappings
.AddFromAssemblyOf<Products.ProductMapping>()
.AddFromAssemblyOf<Transactions.TransactionMapping>()
.AddFromAssemblyOf<Transactions.TransactionProductMapping>()
.AddFromAssemblyOf<Transactions.Transaction2Mapping>()
.AddFromAssemblyOf<Transactions.TransactionWithProductsMapping>();
})
.ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true)) //SchemaUpdate . Execute dla only update
.BuildSessionFactory();
}
}
}