开发者

NHibernate Mapping Attributes + Dirty Checking

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 apprecia开发者_如何学Cted !


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜