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)
-
0
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.
-
0
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?
-
0
To begin a separate unit of work:
UnitOfWorkManager.Begin(TransactionScopeOption.RequiresNew)
-
0
I see. If the "root" UOW is rolled back, will this new one created inside it alse get rolled back?
-
0
Nope. If you use RequiresNew, it's a separate "root" itself.