public void Commit(string username = null) { Context.ChangeTracker.DetectChanges(); List <DbEntityEntry> created = Context.ChangeTracker.Entries().Where(e => e.State == EntityState.Added).ToList(); foreach (var entry in created) { if (entry.Entity is IAudited) { IAudited auditable = entry.Entity as IAudited; auditable.CreatedBy = username ?? ""; auditable.CreationDate = DateTime.Now; } } List <DbEntityEntry> modified = Context.ChangeTracker.Entries().Where(e => e.State == EntityState.Modified).ToList(); foreach (var entry in modified) { if (entry.Entity is IAudited) { IAudited auditable = entry.Entity as IAudited; auditable.LastEditedBy = username ?? ""; auditable.LastEditDate = DateTime.Now; } } Context.SaveChanges(); }
public void AuditUpdate(IAudited <TUserKey> auditedType) { if (!IsAuthenticated) { throw new SecurityAuthenticationException("The current user is not authorized."); } if (!UserId.HasValue) { throw new SecurityAuthenticationException("The current user id is not defined."); } auditedType.Updated = SystemTime; auditedType.UpdatedBy = UserId.Value; }
public void AuditDelete(IAudited <TUserKey> auditedType) { if (!IsAuthenticated) { throw new SecurityAuthenticationException("The current user is not authorized."); } if (!UserId.HasValue) { throw new SecurityAuthenticationException("The current user is not configured correctly."); } auditedType.Updated = SystemTime; auditedType.UpdatedBy = UserId.Value; auditedType.Deleted = SystemTime; auditedType.DeletedBy = UserId; }