For EF 5 use DbContext
‘s ChangeTracker
:
public bool HasUnsavedChanges()
{
return this.ChangeTracker.Entries().Any(e => e.State == EntityState.Added
|| e.State == EntityState.Modified
|| e.State == EntityState.Deleted);
}
For EF 6 use the ChangeTracker.HasChanges()
method which will also detect changes in many to many relationships:
public bool HasUnsavedChanges()
{
return this.ChangeTracker.HasChanges();
}