I\'m trying to find a way to determine of any changes have been made to a database context (DbContext). Note: I\'m using Visual Studio 2012 with Entity Framework 5 on a Win
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();
}