- I am trying to mark the objects for deletion.
-
I have overridden the Savechnages method, and want to get all the entities regardless of their state. The code below is take from a site, the problem is the objects whose properties have not been changed, and simply marked for deletion don’t show up.
public override int SaveChanges(SaveOptions options) { var deletedEntities = __getDeletedEntities(); _softDelete(deletedEntities); return base.SaveChanges(options); } private List<ISoftDelete> __getDeletedEntities() { return ObjectStateManager .GetObjectStateEntries(EntityState.Unchanged) .Select(entry => entry.Entity) .OfType<ISoftDelete>() .Where(e => e.IsDeleted) .ToList();}
Q: Is there any other way rather than using GetObjectStateEntries, to get all entities regardless of state?
EntityState is a flags enums which means that you can combine multiple values when invoking GetObjectStateEntries like this:
On the other hand since the deleted entities are already marked as deleted they will be sent to the database anyways for deletion so I am not sure what you are going to do with them.