Skip to content

Instantly share code, notes, and snippets.

@eramella
Created August 5, 2015 18:20
Show Gist options
  • Save eramella/1d692f2aed273c1c9167 to your computer and use it in GitHub Desktop.
Save eramella/1d692f2aed273c1c9167 to your computer and use it in GitHub Desktop.
Great pattern created by Julie Lerman. This makes it easy to track data modification. The IsDirty property is mainly for Non-Disconnected applications.
public override int SaveChanges(){
foreach (var history in this.ChangeTracker.Entries()
.Where(e => e.Entity is IModificationHystry && (e.State == EntityState.Added || e.State == EntityState.Modified))
.Select(e => e.Entity as IModificationHistory)
){
history.DateModified = DateTime.Now;
if(history.DateCreated == DateTime.MinValue){
history.DateCreated = DateTime.Now;
}
}
int result = base.SaveChanges();
foreach (var history in this.ChageTracker.Entries()
.Where(e => e.Entry is IModificationHistory)
.Select(e => e.Entity as IModificationHistory)){
history.IsDirty = false;
}
return result;
}
public interface IModificationHistory
{
DateTime DateModified { get; set; }
DateTime DateCreated { get; set; }
bool IsDirty { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment