如何关闭NHibernate的自动(脏检查)更新行为? [英] How to turn off NHibernate's automatic (dirty checking) update behaviour?
问题描述
我刚刚发现,如果我从NHibernate会话中获取对象并更改对象的属性,则NHibernate会在提交时自动更新对象,而无需我调用Session.Update(myObj)
!
I've just discovered that if I get an object from an NHibernate session and change a property on object, NHibernate will automatically update the object on commit without me calling Session.Update(myObj)
!
我可以看到这有什么用,但作为默认行为,这似乎很疯狂!
I can see how this could be helpful, but as default behaviour it seems crazy!
更新:我现在了解持久性无知,因此现在显然是首选的选择.我将在这里留下这个令人尴尬的问题,希望对其他亵渎用户有所帮助.
Update: I now understand persistence ignorance, so this behaviour is now clearly the preferred option. I'll leave this now embarrassing question here to hopefully help other profane users.
如何阻止这种情况发生?这是默认的NHibernate行为还是来自Fluent NHibernate的AutoPersistenceModel?
How can I stop this happening? Is this default NHibernate behaviour or something coming from Fluent NHibernate's AutoPersistenceModel?
如果没有办法阻止这种情况,该怎么办?除非我没有指出这一点,否则这种行为似乎会造成混乱.
If there's no way to stop this, what do I do? Unless I'm missing the point this behaviour seems to create a right mess.
我正在使用NHibernate 2.0.1.4和2009年3月18日发布的Fluent NHibernate版本
I'm using NHibernate 2.0.1.4 and a Fluent NHibernate build from 18/3/2009
这个人对吗?他的回答?
我还读到,重写事件监听器可以解决此问题.但是,在这种情况下不会调用IDirtyCheckEventListener.OnDirtyCheck
.有人知道我需要覆盖哪个监听器吗?
I've also read that overriding an Event Listener could be a solution to this. However, IDirtyCheckEventListener.OnDirtyCheck
isn't called in this situation. Does anyone know which listener I need to override?
推荐答案
您可以将Session.FlushMode
设置为FlushMode.Never
.这将使您的操作明确
You can set Session.FlushMode
to FlushMode.Never
. This will make your operations explicit
即:在tx.Commit()
或session.Flush()
上.当然,这仍然会在提交/刷新后更新数据库.如果您不希望出现这种情况,请调用session.Evict(yourObj)
,它将变为瞬态,并且NHibernate不会为此发出任何db命令.
ie: on tx.Commit()
or session.Flush()
. Of course this will still update the database upon commit/flush. If you do not want this behavior, then call session.Evict(yourObj)
and it will then become transient and NHibernate will not issue any db commands for it.
回复您的是的,那个家伙为您提供了更多控制方式的选择.
这篇关于如何关闭NHibernate的自动(脏检查)更新行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!