简体   繁体   English

使用Fluent NHibernate进行非主键Identity AutoIncrement Mapping

[英]Non primary key Identity AutoIncrement Mapping using Fluent NHibernate

I need to manage an additionnal Auto Increment column using Fluent NHibernate. 我需要使用Fluent NHibernate管理一个额外的自动增量列。

All my domain classes use Assigned Guid as ID but in a particular entity i need an additionnal auto increment value. 我的所有域类都使用Assigned Guid作为ID,但在特定实体中我需要一个额外的自动增量值。

I've tried the following mapping, the column is well created in SQL Server but the Identity Specification isn't set. 我尝试了以下映射,该列在SQL Server中很好地创建,但未设置标识规范。

        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 如果你有机会在这个问题的答案之后,你可以在这个SO帖子中找到一些帮助: 流利的nhibernate自动增量非键(Id)属性

In Hibernate: 在Hibernate中:

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

And Fluent NHibernate: 和流利的NHibernate:

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

and potentially this Hibernate forum entry: https://forum.hibernate.org/viewtopic.php?f=1&t=954375 可能这个Hibernate论坛条目: 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. 我假设您正在尝试使用NHibernate中的内置模式生成工具来执行此操作。 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. 因此,除非此列是主键的一部分,否则您需要一个手动方法将其设置为Identity列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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