user78295
user78295

Reputation: 55

Adding new entity in NHibernate PostUpdateEvent

How do I add a new entity object using NHibernate whilst handling a PostUpdateEvent event? I have implemented an audit handler which essentially creates a new object in the OnPostUpdate handler, and attempts to insert into the database.

However, it doesn't actually seem to get committed at any point. If I start a new transaction and commit that in the OnPostUpdate handler, it recurses and gives a stack overflow.

Any ideas?

Thanks,

Jim

Upvotes: 1

Views: 875

Answers (1)

John Rayner
John Rayner

Reputation: 3535

I think that PostUpdateEvent is too late in the processing pipeline for what you want. Examples I've seen (e.g. http://nhforge.org/wikis/howtonh/creating-an-audit-log-using-nhibernate-events.aspx and http://ayende.com/Blog/archive/2009/04/29/nhibernate-ipreupdateeventlistener-amp-ipreinserteventlistener.aspx) use PreUpdateEvent. If you have a database transaction open then you will be sure that the audit details are written only when the main update succeeds, even though you may use pre-update and not post-update events.

Cheers, John

Upvotes: 1

Related Questions