This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hi,
Entity - setup for soft delete. UniqueProperty on entity. This property is both unique in the model and has a unique constraint in the db. The db unique constraint is on (Entity.UniqueProperty, DeletedOn) so you can only have one non-deleted record for that UniqueProperty at any one time. This entity is deleted at the same time we call a cloud service. And because the async calls to the cloud sometimes fail we need to run a manual process to ensure that the entities we consider deleted have also been deleted in the cloud. Architecturally this stinks but we're slowly getting round to fixing that. ;) So I also have another bool property on the entity: EntityRemoteDeleteIsVerified
What I need to be able to do is query for soft deleted entities (done, easy), do the verification, and then update the EntityRemoteDeleteIsVerified property. The problem I'm seeing is that updating the property on a deleted entity flips it back to a Modified state. When I commit, it fails the UniqueProperty checking - since I have a non-deleted entity in the db and a now-not-deleted-but-modified entity in the unitofwork. That's not ideal - I would be like to be able to update a deleted entity for admin purposes. So I thought I would just update the property and re-delete the entity. But (unexpectedly) I find that if I update a property and delete the entity in the same unitofwork, when i commit it completely ignores the property change - only updates the DeletedOn. I was expecting that I could update an entity and soft delete it in the same commit. And I can't do a 2-stage commit (update, delete) because of the unique constraint mentioned earlier. so - questions: 1) any suggested better way to update a soft-deleted entity? because really, that's all i want to do. 2) why can I not update and soft-delete an entity in the same commit? is this by design - not intuitive to me. is there a workaround? |
|
|
1. If you are using an entity, you have to do it in two saves, using a transaction to ensure atomicity: begin transaction, update the entity, save changes, delete the entity, save changes, commit transaction. I understand that this doesn't play nicely with the unique constraint, so you'll need to check to see if your database provides a way to temporarily suspend a constraint during a transaction (and check it only when the transaction commits). However, given your specific scenario, there seems little value in resurrecting the deleted data into an entity. Instead, it sounds like your verification process could get away with doing a bulk update, using the IUnitOfWork.Update method. Update wraps a naive SQL UPDATE statement, and will happly modify soft-deleted rows. 2. For the same reason the SQL DELETE statement doesn't have a SET clause. The semantics of 'delete' are still 'delete,' even though the entity is not being physically scrubbed from the database. The entity is going away: there is nothing to save changes *to*. If you want an update, you have to do it before deleting the entity. |
|
|
fair enough - did almost exactly as you suggested in 1. i had spun up a raw ado.net sqlcommand using the LS context connstr and thrown a sql statement at it. didn't realise there was an update method on the iunitofwork - good stuff. cheers |
|