Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non primary key Identity AutoIncrement Mapping using Fluent NHibernate

I need to manage an additionnal Auto Increment column using Fluent NHibernate.

All my domain classes use Assigned Guid as ID but in a particular entity i need an additionnal auto increment value.

I've tried the following mapping, the column is well created in SQL Server but the Identity Specification isn't set.

        Id(x => x.OrderId).GeneratedBy.Assigned();

        Map(x => x.TicketNumber).ReadOnly().Generated.Always().Not.Nullable();

Any help ?

like image 405
Yoann. B Avatar asked Mar 29 '11 10:03

Yoann. B


3 Answers

On the off-chance you are still after an answer for this question, you may be able to find some help in this SO post: fluent nhibernate auto increment non key (Id) property

In Hibernate:

<property name="Foo" generated="always" update="false" insert="false" />

And Fluent NHibernate:

Map(x => x.Foo).ReadOnly().Generated.Always();

and potentially this Hibernate forum entry: https://forum.hibernate.org/viewtopic.php?f=1&t=954375

"generated means that the value is being generated by the database. Thus you'd need a trigger, etc actually setting these values."

like image 127
fatty Avatar answered Sep 23 '22 18:09

fatty


Map(x => x.TicketNumber).
   .CustomSqlType("INT IDENTITY(1,1)")
   .Not
   .Nullable()
   .ReadOnly()
   .Generated.Insert();
like image 41
MRP Avatar answered Sep 19 '22 18:09

MRP


I'm assuming you're trying to use the built in schema generation tool in NHibernate to do this. Unfortunately with this tool, it is impossible to do what you are asking. The only columns it will set the Identity flag for, are the primary key columns. So unless this column is part of the primary key, you will need a manual method to set it to be an Identity column.

like image 1
Vadim Avatar answered Sep 20 '22 18:09

Vadim