My application uses Entity Framework 6.1.0 and DbContextAPI.
This is some kind of CAD system, and it is intended for editing some technical documents. To detect the fact of changes in the document, I use DbContext.ChangeTracker.HasChanges.
When a document has a large amount of data (approximately 20-25 thousand objects), it DbContext.ChangeTracker.HasChangesworks very slowly . Since this code is used to enable / disable the Save command, it is quite often executed from the user interface thread. This, in turn, affects application performance.
I rewrote this snippet:
private Lazy<DbContext> context;
public bool HasChanges
{
get
{
if (!context.IsValueCreated)
{
return false;
}
return context.Value.ChangeTracker.HasChanges();
}
}
to that:
public bool HasChanges
{
get
{
if (!context.IsValueCreated)
{
return false;
}
var objectStateManager = ((IObjectContextAdapter)context.Value).ObjectContext.ObjectStateManager;
return
objectStateManager.GetObjectStateEntries(EntityState.Added).Any() ||
objectStateManager.GetObjectStateEntries(EntityState.Deleted).Any() ||
objectStateManager.GetObjectStateEntries(EntityState.Modified).Any();
}
}
and (this is a miracle!) everything works very fast.
, DbChangeTracker.HasChanges .
- ?