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();
}
I know as you are using Entity Framework 5, this answer will not help you, but it may help others.
Starting with Entity Framework 6 and in Entity Framework Core you can check for changes simply by following line of code:
context.ChangeTracker.HasChanges()