Created
June 5, 2013 00:04
-
-
Save luizdamim/5710660 to your computer and use it in GitHub Desktop.
NHibernate `ISessionFactory` for in-memory SQLite.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Customer | |
{ | |
public virtual int Id { get; set; } | |
public virtual string FirstName { get; set; } | |
public virtual string LastName { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CustomerMap : ClassMapping<Customer> | |
{ | |
public CustomerMap() | |
{ | |
Table("comments"); | |
this.Id(x => x.Id); | |
Property(x => x.FirstName); | |
Property(x => x.LastName); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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