I've implemented one-to-many code mapping with cascade deletion. I have associated file with child entity. I want to delete file automatically on child cascade deletion. How can it be implemented?
Asked
Active
Viewed 78 times
2
-
1Maybe you could include some code examples you have tried? – Dai Bok Feb 27 '17 at 12:01
1 Answers
1
I found out that NHibernate supports Listeners on Configuration level. It's not perfect, but better than nothing. Simplified example:
var deleteListener = new DeleteListener();
_configuration.SetListener(ListenerType.Delete, deleteListener);
class DeleteListener : DefaultDeleteEventListener {
public override void OnDelete(DeleteEvent e, ISet<object> transientEntities) {
MyEntity entity = e.Entity as MyEntity;
if (entity != null) {
// code for file deletion
}
base.OnDelete(e, transientEntities);
}
}

Dem0n13
- 766
- 1
- 13
- 37
-
1You may use an [`IInterceptor`](http://nhibernate.info/doc/nhibernate-reference/events.html#objectstate-interceptors) too, deriving from `EmptyInterceptor`. But that would probably not be better than [events](http://nhibernate.info/doc/nhibernate-reference/events.html#objectstate-events). – Frédéric Feb 27 '17 at 15:17