public void CompleteEntityData(ITrackableModel trackableEntity, EntityState state) { if (trackableEntity != null) { _userAccessor = ServiceLocator.Current.GetInstance <ICurrentUserAccessor>(); if (state == EntityState.Added) { trackableEntity.InsertDate = DateTime.Now; trackableEntity.LastUpdateDate = DateTime.Now; trackableEntity.LastEditor = _userAccessor.UserId; trackableEntity.Creator = _userAccessor.UserId; } else if (state == EntityState.Modified) { trackableEntity.InsertDate = DateTime.Now; trackableEntity.LastUpdateDate = DateTime.Now; } } }
public void SaveChanges() { foreach (DbEntityEntry item in _context.ChangeTracker.Entries()) { ITrackableModel model = item.Entity as ITrackableModel; if (model != null) { if (item.State == System.Data.Entity.EntityState.Added) { model.Created = DateTime.UtcNow; } else { _context.Entry(model).Property(x => x.Created).IsModified = false; } model.Modified = DateTime.UtcNow; } } this._context.SaveChanges(); }