Search code examples
c#-4.0nhibernate-mappingfluent

Fluent Nhibernate Decimal Primary key with Auto Increment


Is it Possible to created Decimal as a Primery key with Auto Incremented from FNH Mapping??


Solution

  • To achieve auto increment Identity type must be integral int, long, uint, ulong.

    ex :

    public class User { public virtual int Id { get; set; } public virtual IList Names { get; set; } }

    Id(x => x.ID).Column("ID").GeneratedBy.Increment();

    for decimal properties : Id(x => x.ID).Column("ID").GeneratedBy.Assigned();

    Assign the value while creating a new object. Value should be increment of last generated key in your database.

    OR you can just implement a Custom ID Generator for NHibernate. http://lucisferre.net/2009/07/14/implementing-a-custom-id-generator-for-nhibernate/