Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

UPDATE and DELETE in the same UOW causing issues. #4696


User avatar
0
bertusvanzyl created

I have an entity, with soft delete enabled.

I want to set some metadata on this entity, and then delete it.

However, if I call Update on the repository, and then Delete, the update is completely ignored. The entity ends up with IsDeleted set to true, but the updates are not there.

If I call Delete before Update (just trying different stuff, I know this is not proper), the opposite happens. The update is successful, bit IsDeleted is NOT set to true.

Am I doing something wrong here? How can I update the entity, and then delete it immediately?


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

    Inject IUnitOfWorkManager and call _unitOfWorkManager.Current.SaveChanges() between Update and Delete.

    Why: aspnetboilerplate/aspnetboilerplate#2631 (comment)

    <cite>hikalkan: </cite>

    When you delete an entity, ABP reloads it from database (this is the safest way to reset an entity's state), sets IsDeleted = true (and audit properties) and saves again.

    It's not expected to change a property of an entity before delete it. Soft delete should be thought as a real delete.

    In brief, it's by design.

  • User Avatar
    0
    bertusvanzyl created

    That works thank you!

    I have a question though, at one point I did try createing a seperate UOW by doing:

    using (var uow = UnitOfWorkMakager.Begin()) { uow.Complete(); }

    I notice that the uow there does not have SaveChanges. Also, why doesnt calling Complete on that uow, have the same effect as calling SaveChanges on UnitOfWorkManager.Current?

  • User Avatar
    0
    aaron created
    Support Team

    To begin a separate unit of work:

    UnitOfWorkManager.Begin(TransactionScopeOption.RequiresNew)
    
  • User Avatar
    0
    bertusvanzyl created

    I see. If the "root" UOW is rolled back, will this new one created inside it alse get rolled back?

  • User Avatar
    0
    aaron created
    Support Team

    Nope. If you use RequiresNew, it's a separate "root" itself.