开发者

NHibernate - Only persist changes when a save/update call is made

开发者 https://www.devze.com 2023-01-25 01:34 出处:网络
NHibernate auto saves any changes made to an object regardless of whether you make a session.save/update call or not when committing a transaction. EG.

NHibernate auto saves any changes made to an object regardless of whether you make a session.save/update call or not when committing a transaction. EG.

session.BeginTransaction();

User user = repos.getUser("tony");

user.Age = 34

transaction.Commit();

Age is updated. This can be annoying when you want to get an object from the DB and make some changes without those changes being saved. You can session.evict the object, but then you lose the ability to load proxies.

Is there a way to get NHibernate to only persist changes if a save/update call is made?

Update:

Thanks for responses, no one told me how to do it (and it may not be possible) so I am going to leave it unanswered.

You should wrap all db calls in a transaction and commit that transaction to close it.

I am doing this because there is complex sorting I do of the results that isn't really possible via SQL/NHibernate. Unfortunately NHibernate assumes I want to save this new order back to the DB.

My workaro开发者_如何学Gound is to add a property to my transaction attribute [Transaction(onlyread = true)] which causes it to use FlushMode.Never

I think for future work I will just make sure to only make changes that I want to persist. Pity there isn't the option to only save when you explicitly call it.


This is how NHibernate sessions are DESIGNED to work.

If you'd rather do everything manually, use an IStatelessSession instead. You'll lose caching, lazy loading, etc.

0

精彩评论

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

关注公众号