Entity Framework 5 - DbContext Has Changes?
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()
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();
}