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

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

问题描述

是否可以有一个由数据库管理的类自动增量的整数属性,但不是一个主键(或ID作为NHibernate引用他们)?我无法找到如何做到这一点的例子。任何帮助,将不胜感激。

解决方案

两个选项。
$ b $ ul
<如果数据库是100%负责这个你只需要告诉NHibernate的属性是生成的,不包括它在任何更新/ isnerts。缺点是,NH将需要做一个额外的选择,以保持新鲜的内存价值。



<如果数据库不是负责任的,而且您仅仅是一个数据库,那么您就可以使用这个数据库。想要自动完成这个工作,你可以使用一个拦截器在插入时将该值设置为1,并在更新时将其值加1。

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

你可以重载OnSave()来找到属性,设置初始值,然后重写OnFlushDirty()来查找属性的属性和增量。



$ b 编辑:



我是一个白痴,没有注意到你说流利的NHibernate。

$ hr

<编辑#2:

我认为你也可能有兴趣使用这个列作为版本控制?

 <版本名称=Foogenerated =always/> 


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.

解决方案

Two options.

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

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

  • 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 - Interceptors)

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


Edit:

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


Edit #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天全站免登陆