I’m trying to use NHibernate with an existing database. In the data-model there is columns in each table that contains the time and username of the last update made to a row. How do I do this using NHibernate?
I tried to implement a interceptor that sets ChangeTime and ChangeUser in the entities before it gets saved using the IInterceptor.OnSave method. This didn’t work because setting these properties triggers an update to the row even if no other properties has been modified.
It could have worked if there was any way to tell NHibernate to exclude the ChangeTime and ChangeUser properties then it does it’s dirty-checking. But i haven’t found any way to accomplish this.
Thanks for any help.
You should register a listener to the pre insert and pre update events. You can do it through your configuration like so:
and then implement a listener – something like this (might not be entirely accurate – written off my memory):
and do the same thing with the pre update event.
EDIT: This one takes care of insert as well as update and it seems to work.