简体   繁体   中英

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 ?

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM