如何关闭 NHibernate 的自动(脏检查)更新行为? [英] How to turn off NHibernate's automatic (dirty checking) update behaviour?

查看:24
本文介绍了如何关闭 NHibernate 的自动(脏检查)更新行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚发现,如果我从 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 和 18/3/2009 的 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屋!

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