I am soft deleting entities by using a "DeletedDate" attribute. These entities are returned from the server so they can be removed from another user's Breeze cache. What is the proper way to detach soft deleted entities?
One approach I have tried is to watch for entity changes and then detach it if it has a DeletedDate:
// Mark soft-deleted entities as detached
manager.entityChanged.subscribe(function (args) {
if (args && args.entity && args.entity.DeletedDate) {
args.entity.entityAspect.setDetached();
}
});
The problem with this approach is that this occurs when relationships are still being built by Breeze, so the detached entity may still be associated with other objects. For example, detachedEntity.Relationship1 = null (correct behavior), but detachedEntity.Relationship2 != null (incorrect behavior) and detachedEntity.Relationship2.RelatedEntities contains detachedEntity (incorrect behavior.)
I am looking for a solution where I can detach these entities properly and remove it from relationships before the results are passed on. Any ideas?
Example:
I modified the Todo Angular sample to demonstrate the issue. I added a Category entity that has a many-to-many relationship with the TodoItem. The entity TodoItemCategory acts as a mapping table between the entities. The DeletedDate property on each entity represents a soft delete.
In the example the "Essential Needs" category is soft deleted. All users will receive this entity in case it is in their cache and needs to be cleaned up. When I detach it during the entityChanged event, some of its relationships are not removed. The correct behavior would be for the relationships to be removed.
How can I detect a soft-deleted entity and have it properly detached, before the query results are passed on?