Skip to content

Instantly share code, notes, and snippets.

Created October 1, 2011 07:08
Show Gist options
  • Save anonymous/1255717 to your computer and use it in GitHub Desktop.
Save anonymous/1255717 to your computer and use it in GitHub Desktop.
NH 3.2 core mapping
//mapping
public class EntityMapping<T> : ClassMapping<T> where T : IEntity
{
public EntityMapping()
{
DynamicInsert(true);
DynamicUpdate(true);
Lazy(false);
Id(x => x.Id, mapper => mapper.Generator(Generators.Identity));
}
public class UserMap : EntityMapping<Use>
{
public UserMap()
{
Property(u => u.UserName, c => {
c.NotNullable(true);
c.Length(32);
c.UniqueKey("UQ_User_UserName");
});
Property(u => u.Password, c =>
{
c.NotNullable(true);
c.Length(64);
});
Property(u => u.Salt, c =>
{
c.NotNullable(true);
c.Length(64);
});
Property(u => u.Email, c =>
{
c.NotNullable(true);
c.Length(256);
c.UniqueKey("UQ_User_Email");
});
Property(u => u.Locked);
Property(u => u.CreatedAt);
}
}
//building SessionFactory
var mapper = new ModelMapper();
mapper.AddMappings(Assembly.GetAssembly(typeof(some nhibernate infrastructure type)).GetExportedTypes());
var domainMapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
new SettingsFactory();
var cfg = new Configuration();
cfg.DataBaseIntegration(c =>
{
c.Dialect<MsSql2008Dialect>();
c.ConnectionString = _connectionString;
c.BatchSize = 100;
c.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote;
});
cfg.AddMapping(domainMapping);
_sessionFactory = cfg.BuildSessionFactory();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment