Base solution for your next web application
Open Closed

Keep updated audited rows #4332


User avatar
0
bertusvanzyl created

When an entity implements IFullAudited, and it is updated, it updates the LastModiefiedUserId, and LastModificationTime, and does not create a new row. Is it possible to configure it so that a new row is created, so that the old data before the update is preserved as well on an update?


13 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    You can override ApplyAbpConcepts in your DbContextto create a new entity when an IFullAudited entity is modified. But just doing this won't work because primary key is unique. You will also have to handle relationships (foreign keys).

  • User Avatar
    0
    bertusvanzyl created

    I think the way to go is to subscribe to event bus events, and then log changes to entities where applicable. Is there a way to subscribe to the event bus, so that I receive an event for any entity change?

    I can see that I can subscribe to EntityCreatingEventData<TEntity>, for update create and delete, but I have to state the Entity type. Is there to not specify the entity type, so that I receive events for all entity changes?

    thank you.

  • User Avatar
    0
    aaron created
    Support Team

    From the documentation on Handling Base Events:

    you can implement IEventHandler<EventData> to handle all events in the application.

  • User Avatar
    0
    bertusvanzyl created

    Yes I saw that, and I could use that, and then filter out only the Entity related events. But I was wondering if there is a way to get only Entity Creation, Update and Deletion events.

  • User Avatar
    0
    alper created
    Support Team

    You can handle events by type of event data.

    public class ActivityWriter : 
        IEventHandler<TaskCompletedEventData>, 
        IEventHandler<TaskCreatedEventData>, 
        ITransientDependency
    {
        public void HandleEvent(TaskCompletedEventData eventData)
        {
            //TODO: handle the event...
        }
    
        public void HandleEvent(TaskCreatedEventData eventData)
        {
            //TODO: handle the event...
        }
    }
    
  • User Avatar
    0
    bertusvanzyl created

    When I override ApplyAbpConceptsForDeletedEntity in Dbcontext, it gets called when an entity is deleted.

    However, it also sometimes gets called when an entity is deleted, but an exception causes the unit of work to roll back the transaction.

    The error causing the rollback is when I delete an entity before deleteing navigation properties on that entity, which then causes an foreign key violation. When this happens, I can see that ApplyAbpConceptsForDeletedEntity gets called, and then the AbpHandledExceptionData event fires afterwards.

    I think this might be a bug, because I dont think that ApplyAbpConceptsForDeletedEntity should trigger if the entity was not really deleted from the database because of a UOW rollback.

  • User Avatar
    0
    alper created
    Support Team

    it executes when the UOW is completed (or when current transaction is commited)

  • User Avatar
    0
    bertusvanzyl created

    So it should never be called when the UOW is rolled back, only when it it committed? Because in the example above it gets called on a rollback.

  • User Avatar
    0
    aaron created
    Support Team

    "when the UOW is completed <ins>or</ins> when current transaction is committed"

    To be clear, ApplyAbpConceptsForDeletedEntity executes when SaveChanges is called:

    • explicitly, or
    • automatically when the UOW is completed, or
    • automatically when current transaction is committed (i.e. the outermost UOW is completed)

    In cases 1 and 2, a rollback is still possible <ins>after</ins> (but it doesn't get called <ins>on</ins> a rollback).

  • User Avatar
    0
    bertusvanzyl created

    So it is possible for ApplyAbpConceptsForDeletedEntity to be called, even when the entity will not be deleted from the database?

  • User Avatar
    0
    aaron created
    Support Team

    It <ins>is</ins> deleted, but not necessarily committed.

  • User Avatar
    0
    bertusvanzyl created

    But if it is not committed, it will not be deleted. Surely the row will still be in the database if the delete was never comitted?

  • User Avatar
    0
    aaron created
    Support Team

    What's your point?

    ApplyAbpConceptsForDeletedEntity is called (https://github.com/aspnetboilerplate/aspnetboilerplate/blob/6eccadb5d454f8f3d26bbd330f0d1ffd0455cbd6/src/Abp.EntityFrameworkCore/EntityFrameworkCore/AbpDbContext.cs#L198:3hov3him/) [b:3hov3him]base.SaveChanges().