Base solution for your next web application
Open Closed

Repository.DeleteAsync() does not trigger Deleting-EventHandler #8155


User avatar
0
alexanderpilhar created

8.0.0, Angular, .NET Core

I need to set a relation between two entities of type A and B to null before deleting entity of type A. I thought I could do that easily with predefined events but it turns out the deleting event handler does not get triggered (also, see [EntityDeletingEventData does not fire on deleting user #1460](https://github.com/aspnetzero/aspnet-zero-core/issues/1460)).

Right now, I call the deleting event handler manually using the event bus before deleting the entity itself:

public async Task DeleteAsync(int id)
{
    await EventBus.TriggerAsync(new EntityDeletingEventData<A>(new A { Id = id }));

    await _aRepository.DeleteAsync(id);
}

This works as expected but I guess it is not how it is supposed to be done!? Any ideas why the deleting event handler doesn't get triggered properly? Am i missunderstanding something?


2 Answer(s)
  • User Avatar
    1
    musa.demir created

    It fires but after save changes but before committing a transaction.

    See: https://github.com/aspnetzero/aspnet-zero-core/issues/1460#issuecomment-409478282 https://aspnetboilerplate.com/Pages/Documents/EventBus-Domain-Events#entity-changes

  • User Avatar
    0
    alexanderpilhar created

    I see, thanks @demirmusa!

    I thought I could use the deleting event handler in order to avoid a foreign-key constraint when deleting entity of type A. But it doesn't work like that. I guess, I'll better be using a custom event handler instead of the deleting event handler.