我有问题NHibernate更新我的一些实体时,这不应该发生(脏检查)。 当我使用NHibernate.Mapping.Attributes来映射我的类时,我发现有一个参数“Check”到NHMA的元素“Class”。 我想知道我是否可以通过将此参数设置为false或其他东西来关闭脏检查(此参数的必需类型是字符串,因此可能不是这样)。
任何帮助,将不胜感激 !
I have problems with NHibernate updating some of my entities when this is not supposed to happen (dirty checking). As I use NHibernate.Mapping.Attributes to map my classes, I have found that there is a parameter "Check" to the element "Class" of NHMA. I would like to know if I can turn off dirty checking by setting this parameter to false or something (the required type for this parameter is string, so it may not be that).
Any help would be appreciated !
最满意答案
首先,这不是你应该做的事情。 NHibernate尝试最好的做脏检查正确的东西。 如果NHibernate认为它很脏,那可能就是这样。 试着找出你对实体做出了哪些改变,导致NHibernate认为它很脏,看看你是否可以通过调整它来解决你的问题。
也就是说,有一个解决方案。 NHibernate使用在插入和更新之前触发的侦听器。 有关这方面的更多信息, 请访问 http://ayende.com/Blog/archive/2009/04/29/nhibernate-ipreupdateeventlistener-amp-ipreinserteventlistener.aspx,http://www.codinginstinct.com/2008/04/ nhibernate-20-events-and-listeners.html和许多其他位置 。
事件侦听器本身具有返回值。 这个返回值的作用是告诉NHibernate是否执行实际的插入/更新SQL查询。 当您返回false ,它会执行它们。 当你返回true ,它不会。 这样就可以抑制数据库的实际持久性。
这种方法的好处在于,在NHibernate方面,它认为实体实际上是持久化的,因此NHibernate的内部状态保持正确并且实体变得不那么脏。
Firstly, this is not something you should do. NHibernate tries it very best to do stuff like dirty checking correct. If NHibernate thinks it's dirty, it probably is. Try to find out what changes you've made to the entity which cause NHibernate to think it's dirty and see whether you can solve your issue by tuning this.
That said, there is a solution. NHibernate uses listeners that fire before insert and update. More information on this can be found at http://ayende.com/Blog/archive/2009/04/29/nhibernate-ipreupdateeventlistener-amp-ipreinserteventlistener.aspx, http://www.codinginstinct.com/2008/04/nhibernate-20-events-and-listeners.html and many other locations.
The event listeners themselves have a return value. What this return value does is tell NHibernate whether to execute the actual insert/update SQL queries. When you return false, it executes them. When you return true, it does not. This way you can suppress the actual persistence to the database.
The nice thing of this approach is that where NHibernate is concerned, it believes that the entities actually were persisted, so the internal state of NHibernate stays correct and the entities become not-dirty.
更多推荐
发布评论