流畅的 nhibernate 自动增量非键 (Id) 属性 [英] fluent nhibernate auto increment non key (Id) property

查看:28
本文介绍了流畅的 nhibernate 自动增量非键 (Id) 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能有一个由数据库管理的类自动增量的整数属性,但不是主键(或 NHibernate 所指的 Id)?我很难找到如何执行此操作的示例.任何帮助,将不胜感激.谢谢.

Is it possible to have an integer property of a class auto increment managed by the database but not be a primary key (or Id as NHibernate refers to them)? I'm having trouble finding examples of how to do this. Any help would be appreciated. Thanks.

推荐答案

两个选项.

  • 如果数据库 100% 对此负责,您只需要告诉 NHibernate 该属性已生成,并且不要将其包含在任何更新/isnerts 中.缺点是 NH 需要做一个额外的选择来保持内存中的值.

  • if the database is 100% responsible for this you just need to tell NHibernate that the property is generated and not to include it in any updates/isnerts. The downside is that NH will need to do an additional select to keep the value fresh in memory.

<属性名称="Foo"generated="always" update="false" insert="false"/>

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

如果数据库不负责并且您只想自动完成此操作,您可以使用拦截器在插入时将值设置为 1,并在更新时将其增加 1.

if the database is not responsible and you just want to have this done automatically you can use an interceptor to set the value to 1 on an insert and to increment it by 1 on an update.

http://www.nhforge.org/doc/nh/en/index.html#objectstate-interceptors(11.1 - 拦截器)

http://www.nhforge.org/doc/nh/en/index.html#objectstate-interceptors (11.1 - Interceptors)

您将覆盖 OnSave() 以查找属性并设置初始值,然后覆盖 OnFlushDirty() 以查找属性并递增.

You would override OnSave() to find the property and set the initial value and then override OnFlushDirty() to find the property property and increment.

我是个白痴,没注意到你说的是 Fluent NHibernate.

I'm an idiot, didn't notice you said Fluent NHibernate.

编辑#2:

我想您可能也有兴趣将此列用作版本控制?

I think you might also be interested in using this column as a versioning?

< version name="Foo" generated="always" />

这篇关于流畅的 nhibernate 自动增量非键 (Id) 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆