Question

When removing a child entity from the parent entity collection I noticed that EF sets the childs entity state to be modified instead of deleted.

Is there another property within the entity object manager which sets it to be deleted?

Below is the code I am using within my Save method in the EF repository to find any child items that have been modified and then delete them.

Dim orphans = DataContextFactory.GetWMSDBContext().ChangeTracker.Entries().Where(Function(e) (e.State = EntityState.Modified) AndAlso TypeOf e.Entity Is StockTransferItem)
For Each orphan In orphans
    orphan.State = EntityState.Deleted
Next

The problem I am having is trying to detect the difference between updating and deleting because the state is set to modified for both. At the moment it is deleting the item when I update the record as well. Can I detect between the two states?

Was it helpful?

Solution

The EF says the object is modified rather than deleted because removing it from a parent collection is modifying it, not deleting it. To delete it, use the DeleteObject() method on the object set which contains the object you want deleted, something like:

myObjectContext.StockTransferItems.DeleteObject(stockTransferItemToDelete);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top