Skip to content

Instantly share code, notes, and snippets.

@luizdamim
Created June 5, 2013 00:04
Show Gist options
  • Save luizdamim/5710660 to your computer and use it in GitHub Desktop.
Save luizdamim/5710660 to your computer and use it in GitHub Desktop.
NHibernate `ISessionFactory` for in-memory SQLite.
public class Customer
{
public virtual int Id { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
}
public class CustomerMap : ClassMapping<Customer>
{
public CustomerMap()
{
Table("comments");
this.Id(x => x.Id);
Property(x => x.FirstName);
Property(x => x.LastName);
}
}
public class NHibernateConfig
{
public static ISessionFactory SessionFactory()
{
Configuration cfg = new Configuration();
cfg.Proxy(p => p.ProxyFactoryFactory<NHibernate.Bytecode.DefaultProxyFactoryFactory>());
cfg.DataBaseIntegration(db =>
{
db.Dialect<NHibernate.Dialect.SQLiteDialect>();
db.Driver<NHibernate.Driver.SQLite20Driver>();
// db.ConnectionStringName = @"Data Source=:memory:; Version=3; New=True;";
// db.ConnectionString = @"FullUri=file::memory:?cache=shared";
db.ConnectionString = @"Data Source=:memory:; Version=3;";
db.BatchSize = 200;
db.LogFormattedSql = true;
db.LogSqlInConsole = true;
db.HqlToSqlSubstitutions = "true 1, false 0, yes 'Y', no 'N'";
});
cfg.SessionFactory().GenerateStatistics();
var mapper = new ModelMapper();
mapper.AddMappings(Assembly.GetExecutingAssembly().GetExportedTypes());
mapper.AddMappings(typeof(Customer).Assembly.GetTypes());
mapper.
HbmMapping domainMapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
cfg.AddMapping(domainMapping);
SchemaExport(cfg);
return cfg.BuildSessionFactory();
}
public static void SchemaExport(Configuration config)
{
new SchemaExport(config).Execute(true, true, false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment