Wednesday, January 4, 2012

NHibernate mapping by code a sequence generator

How to map an identifier when you want to use a sequence generator, like the one used by PosrgreSQL ?
Here is an example in the loquacious way using the ClassMapping<T> base class, and, of course, the MapperModel:

   public sealed class MyEntityMap : ClassMapping<MyEntity>
   {
      public MyEntityMap ()
      {
         Table("my_table");
         Id(x => x.Id,
            a =>
            {
               a.Column("my_id");
               a.Generator(Generators.Sequence, g => g.Params(new
               {
                  sequence = "my_sequence_generator"
               }));
            });
      }
   }

Happy mapping ^^