流利的nhibernate自动递增非键

流利的nhibernate自动递增非键

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

问题描述

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

解决方案

两个选项。
$ b $ ul


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

(11.1 - 拦截器)

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



$ b 编辑:



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

$ hr


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

 <版本名称=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)属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 19:58