Skip to content

Instantly share code, notes, and snippets.

@tarasn
Created August 12, 2013 11:31
Show Gist options
  • Save tarasn/6210101 to your computer and use it in GitHub Desktop.
Save tarasn/6210101 to your computer and use it in GitHub Desktop.
using System.Reflection;
using FluentNHibernate;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
namespace NHibernateExperiments.Fluent
{
public class InMemoryDatabaseFluent
{
private Configuration _configuration;
public ISession Session { get; private set; }
public InMemoryDatabaseFluent()
{
var sessionFactory = CreateSessionFactory();
Session = sessionFactory.OpenSession();
BuildSchema();
}
private void BuildSchema()
{
var export = new SchemaExport(_configuration);
export.Execute(true, true, false, Session.Connection, null);
}
private ISessionFactory CreateSessionFactory()
{
var connectionString = string.Format("data source=:memory:");
IPersistenceConfigurer persistenceConfigurer = SQLiteConfiguration.Standard
.ShowSql()
.ConnectionString(connectionString);
_configuration = persistenceConfigurer
.ConfigureProperties(new Configuration());
var persistenceModel = new PersistenceModel();
var assembly = Assembly.GetAssembly(typeof(InMemoryDatabaseFluent));
persistenceModel.AddMappingsFromAssembly(assembly);
persistenceModel.Configure(_configuration);
return _configuration.BuildSessionFactory();
}
public void Dispose()
{
Session.Dispose();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment