user1858996
user1858996

Reputation:

Fluent Nhibernate Decimal Primary key with Auto Increment

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

Upvotes: 1

Views: 1565

Answers (2)

Levi
Levi

Reputation: 869

I ran across the same thing. It appears that fluent-nhibernate is enforcing this constraint.

If you know that the database will allow this (in this case, it should be fine) you can just bypass the constraint like so:

Id(x => x.MyDecimalId).GeneratedBy.Custom<IdentityGenerator>();

This will use the same strategy as Identity() would have, but not raise an exception. In my case, I've added an extension method on IdentityGenerationStrategyBuilder<IdentityPart> as such:

public static class IdentityPartExtensions
{
    public static void NumericIdentity(this IdentityGenerationStrategyBuilder<IdentityPart> identityBuilder)
    {
        identityBuilder.Custom<IdentityGenerator>();
    }
}

So then you can just do:

Id(x => x.MyDecimalId).GeneratedBy.NumericIdentity();

Upvotes: 2

Deepak
Deepak

Reputation: 420

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/

Upvotes: 0

Related Questions