private void LogEnteryDetails(ObjectStateEntry dbEntry, AuditLog auditLog, AuditingWidget widget) { if (dbEntry.State == EntityState.Added || dbEntry.State == EntityState.Deleted) { bool isAdd = dbEntry.State == EntityState.Added; var values = isAdd ? dbEntry.CurrentValues : dbEntry.OriginalValues; for (int i = 0; i < values.FieldCount; i++) { object objValue = values.GetValue(i); string value = objValue == null ? null : objValue.ToString(); if (!string.IsNullOrEmpty(value)) { var d = new AuditLogDetail() { Id = Guid.NewGuid(), AuditLogId = auditLog.Id, PropertyName = values.GetName(i), NewValue = isAdd ? value : null, OriginalValue = isAdd ? null : value, }; auditLog.AuditLogDetails.Add(d); widget.ChangeTrackerContext.AuditLogDetails.Add(d); } } } else if (dbEntry.State == EntityState.Modified) { for (int i = 0; i < dbEntry.CurrentValues.FieldCount; i++) { // For updates, we only want to capture the columns that actually changed var originalValue = dbEntry.OriginalValues.GetValue(i); var currentValue = dbEntry.CurrentValues.GetValue(i); if (!object.Equals(originalValue, currentValue)) { var originalString = originalValue == null ? null : originalValue.ToString(); var newString = currentValue == null ? null : currentValue.ToString(); if (!string.IsNullOrEmpty(originalString) || !string.IsNullOrEmpty(newString)) { var d = new AuditLogDetail() { Id = Guid.NewGuid(), AuditLogId = auditLog.Id, PropertyName = dbEntry.OriginalValues.GetName(i), OriginalValue = originalString, NewValue = newString }; auditLog.AuditLogDetails.Add(d); widget.ChangeTrackerContext.AuditLogDetails.Add(d); } } } } }
private void InsertFakeLegacyLog() { var options = new DbContextOptionsBuilder <TestTrackerContext>() .UseSqlServer(TestConnectionString) .Options; var log = new AuditLog { TypeFullName = "ModelWithCustomTableAndColumnNames", EventType = EventType.Added, RecordId = rdg.Get <int>().ToString(), EventDateUTC = rdg.Get <DateTime>(), UserName = "" }; var magnitudeLogDetail = new AuditLogDetail { Log = log, NewValue = rdg.Get <string>(), OriginalValue = rdg.Get <string>(), PropertyName = "MagnitudeOfForce" }; var directionLogDetail = new AuditLogDetail { Log = log, NewValue = rdg.Get <int>().ToString(), OriginalValue = rdg.Get <int>().ToString(), PropertyName = "Direction" }; var subjectLogDetail = new AuditLogDetail { Log = log, NewValue = rdg.Get <string>(), OriginalValue = rdg.Get <string>(), PropertyName = "Subject" }; using (TestTrackerContext ttc = new TestTrackerContext(options)) { ttc.AuditLogs.Add(log); ttc.AuditLogDetails.Add(magnitudeLogDetail); ttc.AuditLogDetails.Add(directionLogDetail); ttc.AuditLogDetails.Add(subjectLogDetail); ttc.SaveChanges(); } }
public async Task AddOrUpdate <T>(T entity, params string[] ignoreProperties) where T : BaseEntity { if (entity == null || Entry(entity).State == EntityState.Added || Entry(entity).State == EntityState.Modified) { return; } var state = await Set <T>().AnyAsync(x => x.Id == entity.Id) ? EntityState.Modified : EntityState.Added; Entry(entity).State = state; var type = typeof(T); RelationshipManager relationship; var stateManager = ((IObjectContextAdapter)this).ObjectContext.ObjectStateManager; if (stateManager.TryGetRelationshipManager(entity, out relationship)) { foreach (var end in relationship.GetAllRelatedEnds()) { var isForeignKey = end.GetType().GetProperty("IsForeignKey", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(end) as bool?; var navigationProperty = end.GetType().GetProperty("NavigationProperty", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(end); var propertyName = navigationProperty?.GetType().GetProperty("Identity", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(navigationProperty) as string; if (string.IsNullOrWhiteSpace(propertyName) || ignoreProperties.Contains(propertyName)) { continue; } var property = type.GetProperty(propertyName); if (property == null) { continue; } if (end is IEnumerable) { await UpdateChildrenInternal(entity, property, isForeignKey == true); } else { await AddOrUpdateInternal(entity, property, ignoreProperties); } } } if (state == EntityState.Modified) { Entry(entity).OriginalValues.SetValues(await Entry(entity).GetDatabaseValuesAsync()); Entry(entity).State = AuditLogDetail.GetChangedProperties(Entry(entity)).Any() ? state : EntityState.Unchanged; } }
private void RenameColumnNameToPropertyName(AuditLogDetail auditLogDetail, string propertyName) { string oldName = auditLogDetail.PropertyName; if (oldName == propertyName) { return; } auditLogDetail.PropertyName = propertyName; _trackerContext.SaveChanges(); OnAuditLogDetailUpdated(new NameChangedEventArgs { OldName = oldName, NewName = propertyName, RecordId = auditLogDetail.Id }); }
private void LogRelatedEntryDetails(string refrencePropertyName, EntityKey refrenceKey, AuditLog auditLog, AuditingWidget widget, bool isAddedRelation) { string value = refrenceKey.EntityKeyValues == null ? ENTITYKEYPLACEHOLDER : refrenceKey.EntityKeyValues.First().Value.ToString(); var auditLogDetail = new AuditLogDetail() { Id = Guid.NewGuid(), AuditLogId = auditLog.Id, PropertyName = refrencePropertyName, NewValue = isAddedRelation ? value : null, OriginalValue = isAddedRelation ? null : value }; auditLog.AuditLogDetails.Add(auditLogDetail); widget.ChangeTrackerContext.AuditLogDetails.Add(auditLogDetail); widget.TrackedEntityDetails.Add(auditLogDetail, refrenceKey); }
private void InsertFakeLegacyLog() { var log = new AuditLog { TypeFullName = "ModelWithCustomTableAndColumnNames", EventType = EventType.Added, RecordId = RandomNumber.ToString(), EventDateUTC = RandomDate, UserName = "" }; var magnitudeLogDetail = new AuditLogDetail { Log = log, NewValue = RandomText, OriginalValue = RandomText, PropertyName = "MagnitudeOfForce" }; var directionLogDetail = new AuditLogDetail { Log = log, NewValue = RandomNumber.ToString(), OriginalValue = RandomNumber.ToString(), PropertyName = "Direction" }; var subjectLogDetail = new AuditLogDetail { Log = log, NewValue = RandomText, OriginalValue = RandomText, PropertyName = "Subject" }; Db.AuditLog.Add(log); Db.LogDetails.Add(magnitudeLogDetail); Db.LogDetails.Add(directionLogDetail); Db.LogDetails.Add(subjectLogDetail); Db.SaveChanges(); }