Is there a way to find out whether there are unsaved changes in my entity context, in the Entity Framework?
Starting with EF 6, there is context.ChangeTracker.HasChanges()
.
This might work (if by changes you mean added, removed and modified entities):
bool changesMade = (context.ObjectStateManager.GetObjectStateEntries(EntityState.Added).Count() + context.ObjectStateManager.GetObjectStateEntries(EntityState.Deleted).Count() + context.ObjectStateManager.GetObjectStateEntries(EntityState.Modified).Count() ) > 0;
Edit:
Improved code:
bool changesMade = context. ObjectStateManager. GetObjectStateEntries(EntityState.Added | EntityState.Deleted | EntityState.Modified ).Any();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With