Lev
Lev

Reputation: 434

How to make NHibernate auto-save dirty entities?

I have a situation where I need persist every change in my entities, because changing entity properties physically moves things (or rather reflects that), so if the application is closed (at any time, by close or kill) and started again, it must restart where it left.

Basically what I want is to flush the session each time something becomes dirty. So far I looked at interceptors, but found nothing which suits my needs.

Of course I can do that manually by calling Flush() on every property change, but I would want to avoid introducing persistence code into my entities.

Any hints?

Upvotes: 0

Views: 554

Answers (1)

Anton Gogolev
Anton Gogolev

Reputation: 115751

There's no such thing built in into NHibernate (FlushMode.Always flushes only when you re-query), but even if there were, I wouldn't recommend that. You may very well end up with your data being in an inconsistent state. To mitigate this, wrap all your "data-modifying" code in transactions and commit them accordingly.

Upvotes: 1

Related Questions