using System.Collections.Generic;
using NHibernate.Mapping.ByCode;
using NHibernate.Mapping.ByCode.Impl;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Tool.hbm2ddl;
using Community.CsharpSqlite.SQLiteClient;
public static void Main()
var conn=new SQLiteConnection();
var cfg=ConfigureNHibernate();
var sessionFactory=cfg.BuildSessionFactory();
var session = sessionFactory.OpenSession();
new SchemaExport(cfg).Execute(true, true, false, session.Connection, null);
private static Configuration ConfigureNHibernate()
var configure = new Configuration();
configure.SessionFactoryName("BuildIt");
configure.DataBaseIntegration(db =>
db.Dialect<NHibernate.Dialect.SQLiteDialect>();
db.Driver<NHibernate.Driver.SQLite20Driver>();
db.ConnectionString = "Data Source=:memory:;Version=3;New=True;Pooling=True;Max Pool Size=1;";
db.ConnectionReleaseMode = ConnectionReleaseMode.OnClose;
var mapping = GetMappings();
configure.AddDeserializedMapping(mapping, "NHSchemaTest");
private static HbmMapping GetMappings()
var mapper = new ModelMapper();
mapper.Class<Person>(e =>
e.Id(c => c.Id, c => c.Generator(Generators.GuidComb));
e.Set(c => c.Properties, c => {
c.Key(k=>k.Column("PersonId"));
mapper.Class<PersonProperty>(e =>
e.Id(c => c.Id, c => c.Generator(Generators.GuidComb));
e.ManyToOne(c => c.Owner);
var hbmMapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
public class PersonProperty
public virtual Person Owner{get;set;}
Properties = new HashSet<PersonProperty>();
private ICollection<PersonProperty> _properties;
public virtual ICollection<PersonProperty> Properties
public virtual void AddProperty(PersonProperty property)
_properties.Add(property);
public virtual void RemoveProperty(PersonProperty property)
_properties.Remove(property);