Created
August 5, 2015 18:20
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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