NHibernate - Save changes only when saving / updating is called

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

session.BeginTransaction(); User user = repos.getUser("tony"); user.Age = 34 transaction.Commit(); 

Age updated. This can be annoying when you want to get an object from the database and make some changes without saving these changes. You can session.evict the object, but then you lose the ability to download proxies.

Is there a way to force NHibernate to save changes only when saving / updating is called?

Update:

Thanks for the answers, no one told me how to do this (and it may be impossible), so I'm going to leave it unanswered.

You must wrap all db calls in a transaction and commit the transaction in order to close it.

I do this because there is a complex sorting of results that are actually not possible with SQL / NHibernate. Unfortunately, NHibernate assumes that I want to save this new order back to the database.

My workaround is to add a property to my transaction attribute [Transaction (onlyread = true)], which forces it to use FlushMode.Never

I think that for future work I will just keep track of the changes that I want to keep. It is a pity that there is an opportunity not only to save when you explicitly call it.

+4
source share
1 answer

This is how NHibernate works for DESIGNED.

If you prefer to do everything manually, use IStatelessSession . You will lose caching, lazy loading, etc.

+3
source

All Articles