Removing entities from the EntityManager doesn't delete them. It just removes them from the cache. To delete them you have to call the Delete method on the EntityAspect:
a.EntityAspect.Delete() or
EntityAspect.Wrap(a).Delete() if you do code-first and don't have the EntityAspect property.
Now, keep in mind, the entity is still in the cache at this point, marked for deletion. Once you call SaveChangesAsync, the corresponding record will be deleted from the database and then the entity will get removed from the cache.
The typical logic for handling checkboxes is if the user unchecks an item, you call EntityAspect.Delete to mark it for deletion. If the user changes their mind and checks it again, you call EntityAspect.RejectChanges, to undelete the entity.
If the user checks a new item that hadn't been checked before, you create a new entity and add it to the EntityManger. Now here comes the tricky part. If the user changes their mind, you call EntityAspect.Delete(). Because you called Delete on an Added entity, the entity isn't marked for deletion, but simply detached from the EntityManager, because the entity doesn't exist in the data source yet. So, now if the user changes their mind again and checks the item again, you can either add the detached entity back to the EntityManager with EntityAspect.AddToManager() or you can create an entire new entity and add it.
Hope this helps.
|