如何判断DataContext有修改:

EF6的

using (var db = new Model1())
{
if (db.ChangeTracker.HasChanges())
{
Console.WriteLine("Something has changed");
}
}

 

EF5中:

public bool HasUnsavedChanges()
{
return this.ChangeTracker.Entries().Any(e => e.State == EntityState.Added
|| e.State == EntityState.Modified
|| e.State == EntityState.Deleted);
}

 

 

EF4中为:

 

public Boolean HasUnsavedChanges()
{
return (this.ObjectContext().ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified | EntityState.Deleted).Any());
}

 

DataContext如何放弃修改的建议:

 

 

参考

Entity Framework 5 - DbContext Has Changes

Entity Framework 6.1 is context dirty

04-16 14:57