Yoann. B
Yoann. B

Reputation: 11143

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 ?

Upvotes: 6

Views: 8205

Answers (3)

MRP
MRP

Reputation: 609

Map(x => x.TicketNumber).
   .CustomSqlType("INT IDENTITY(1,1)")
   .Not
   .Nullable()
   .ReadOnly()
   .Generated.Insert();

Upvotes: 2

fatty
fatty

Reputation: 2513

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."

Upvotes: 5

Vadim
Vadim

Reputation: 17957

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.

Upvotes: 1

Related Questions