NHibernate Mapping Attributes + Dirty Checking

橙三吉。 提交于 2020-01-05 07:55:08

问题


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 !


回答1:


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.



来源:https://stackoverflow.com/questions/4035241/nhibernate-mapping-attributes-dirty-checking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!