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, is there an event for 'onDeleted' or 'onDeleting' for items that are makred as softdelete? I'm on LS 3, i think this could be a hug benefit. |
|
|
Not directly, but there are a couple of hooks that you could use to figure this out. One way is to override OnEntityStateChanged or handle the EntityStateChanged changed. I believe that a change from Deleted to Default will always represent a successful save of a pending delete, whether soft or hard. Another is to override OnSaving or handle the Saving event. These occur when LightSpeed is about to save an entity, and you can check EntityState to see if the save is a delete. However, it's possible for the save to fail after this point, for example if a subsequent entity is found to be invalid, so you'd need to detect that case. |
|
|
Thanks Ivan. I am overiding the onsaving event but it doesn't fire if the method of deletion is something like this: var query = new Query(typeof(InventoryItemSample), Entity.Attribute("Id") == thisInventorySampleID); uow.Remove(query); uow.SaveChanges(); Any suggestions / work arounds for this?
|
|
|
No. Remove by query bypasses the entity system. No entities are materialised. Therefore there are no entities to hang event hooks on. If you want to handle entity events, you must materialise entities. Do a Find with the same query, then call Remove for each returned entity. (Just to reinforce this, LightSpeed doesn't even know what gets deleted in a remove-by-query! It just translates your query into SQL and sends it blindly off to the database.) |
|
|
Thanks Ivan. Didn't think it would be possible, I guess implementing some event hooks has to come at the cost of minor performance loss. |
|
|
I'm soft-deleting an entity which is inherrited from a base entity placed on the model to force all columns to be synchronized in DB. I need to set more columns than DeletedDate, like Deleted field as our DB contains this field on each table, but also DeletedBy (not string, but some Guid type). Overriding OnSaving event on base entity doesn't seem to work, all it does is that it increments also LockVersion column. Please help.
|
|
|
We don't save pending changes as part of a delete, except for the LightSpeed-generated ones that are part of marking the entity as soft deleted. At the moment, if you want to modify the entity as part of the soft delete, you must update the entity with your desired changes, save those changes, then delete the entity and save that change as well. You can of course put a transaction around this to ensure atomicity. |
|
|
Thanks for your reply Ivan. Unfortunatelly this solution brings us some overhead. Tables are really huge and running update twice is far from ideal scenario. Suppose you have lots of relations and you want to perform cascade deletes. In this case I would need to find and update all related objects first, then run (soft)delete again, which is awkward. It would be very nice to have possibility to hook the OnDeleting event and set other fields optionally. Am I the only one who would appreciate this feature? |
|
|
An OnDeleting hook wouldn't help with cascade deletes, because rows being cascade deleted may not have been materialised as entities (if the association is lazy loaded and has not been loaded), so there is no object to hook. We would rather need to provide some sort of mechanism by which you could request values to be updated during a soft delete at the class level, so that we could inject those values even though we didn't have an entity. I'll need to have a think about whether this is achievable, and whether we can do it in a way that would be useful for other cases. |
|