(Note for triage: I personally see this as a 'future' work item given where we are but it should be a nice addition and an easy to accept contribution)
The canonical scenario for this is to be able to ask the user if it is ok to close a window that has a context associated with it. If the context does not contain any pending change there is no point in asking the user.
Another very common scenario is to re-calculate the state of the UI based on whether any user action, e.g. to decide whether the "save" button should be enabled or disabled.
The typical way this can be implemented today with the DbContext API is by performing an in-memory LINQ query on the Entries, e.g.:
```
var hasChanges = context.ChangeTracker.Entries().Any(e => (e.State & (EntityState.Added | EntityState.Modified | EntityState.Deleted)) != default(EntityState));
```
However this implementation can quickly become very expensive and make the UI unresponsive, especially when there are many unchanged objects tracked by the context.
Provided lower level access to the internal state we could implement something much more efficient, perhaps something in these lines (using pseudo-code for brevity):
```
public bool HasChanges()
{
if (!ObjectContextInUse == null) {
return false;
}
var stateManager = ObjectContext.ObjectStateManager;
stateManager.DetectChanges();
return stateManager.AddedEntityStore.Any()
&& stateManager.ModifiedEntityStore.Any()
&& stateManager.DeletedEntityStore.Any();
}
```
___Things we would need to decide:___
_Location:_ we could put this on either ChangeTracker or on DbContext directly
_Naming:_ Pick one between HasChanges, IsDirty and changing the signature of DetectChanges to return a result
_Return value:_ bool or int containing number of pending changes (analogous to SaveChanges result)
The canonical scenario for this is to be able to ask the user if it is ok to close a window that has a context associated with it. If the context does not contain any pending change there is no point in asking the user.
Another very common scenario is to re-calculate the state of the UI based on whether any user action, e.g. to decide whether the "save" button should be enabled or disabled.
The typical way this can be implemented today with the DbContext API is by performing an in-memory LINQ query on the Entries, e.g.:
```
var hasChanges = context.ChangeTracker.Entries().Any(e => (e.State & (EntityState.Added | EntityState.Modified | EntityState.Deleted)) != default(EntityState));
```
However this implementation can quickly become very expensive and make the UI unresponsive, especially when there are many unchanged objects tracked by the context.
Provided lower level access to the internal state we could implement something much more efficient, perhaps something in these lines (using pseudo-code for brevity):
```
public bool HasChanges()
{
if (!ObjectContextInUse == null) {
return false;
}
var stateManager = ObjectContext.ObjectStateManager;
stateManager.DetectChanges();
return stateManager.AddedEntityStore.Any()
&& stateManager.ModifiedEntityStore.Any()
&& stateManager.DeletedEntityStore.Any();
}
```
___Things we would need to decide:___
_Location:_ we could put this on either ChangeTracker or on DbContext directly
_Naming:_ Pick one between HasChanges, IsDirty and changing the signature of DetectChanges to return a result
_Return value:_ bool or int containing number of pending changes (analogous to SaveChanges result)