Similar to modifying an entity, deleting an entity is a two step process. You first mark the entity for deletion in the EntityManager's cache, and then you commit the changes to the datasource.
You mark an entity for deletion by calling EntityAspect.Delete.
C# | product.EntityAspect.Delete(); |
VB | product.EntityAspect.Delete() |
After calling Delete, the EntityState will be set to Deleted.
The entity will be deleted from the datasource when you call EntityManager.SaveChanges.
After a successful SaveChanges, the EntityState will become Detached and the reference to the entity is no longer valid and it should not be used.
Calling Delete on a newly created entity (one that has not been saved) will immediately discard the entity since it does not exist in the datasource.
If an entity is marked for deletion, you can call EntityAspect.RejectChanges to undo the scheduled deletion. (You can also call EntityManager.RejectChanges to revert everything in the cache.) Once SaveChanges is successfully called, you can no longer reject the changes, and another save will be required to modify the datasource. To learn more about undoing changes in the client-side cache see Undo changes.
If the Entity Data Model and the database have been configured to handle cascaded deletes, other dependent entities may be deleted as well. For example, deleting an Order may also result in the deletion of all of its OrderDetails. See Handle cascaded deletes for more information.
If you access the properties of the deleted entity, they will be the original values as they were queried from the datasource, as this represents the entity that is being deleted.