public void QueueContextChange <T>(T entry) where T : class { if (entry == null || ctx == null) { return; } //var entry = Clone<T>(original); CheckDisposedAndKillOnDispose(); if (ChangeLog_Entry.GetID_Counter() == 0) { long?max = null; if (logger.ctx.RepositoryChangeLog.Select(l => l.RepositoryChangeLogId).Count() > 0) { max = logger.ctx.RepositoryChangeLog.Max(m => m.RepositoryChangeLogId); } if (max.HasValue && max > 0) { ChangeLog_Entry.SetID_Counter(max.Value); } else { ChangeLog_Entry.SetID_Counter(0); } } var operation = GetOperationType(entry); var entity = new ContextChange_DTO { Entity = entry, Operation = operation, IsCommitted = false }; var type = typeof(T); var tableName = Repository_Helper.GetTableName(type, ctx); var props = type.GetProperties(); var first = props.First(); var last = props.Last(); var pk = GetKeyValues(GetKeyNames(type), entry).FirstOrDefault(); if (operation == OperationType.Insert) { foreach (var prop in props) { var val = prop.GetValue(entry); if (val == null) { if (entity.EndingChangeLogID == 0 && prop == last) { entity.EndingChangeLogID = ChangeLog_Entry.GetID_Counter() - 1; } continue; } else { var changeLogEntry = new ChangeLog_Entry { FQAType = entry.GetType().AssemblyQualifiedName, TableUpdated = tableName, PrimaryKey = null, ColumnUpdated = Repository_Helper.GetColumnName(type, ctx, prop.Name), NewValue = val.ToString(), OldValue = null, Operation = "Insert", CommittedAt = null, ErrorAt = null, ErrorNotes = null }; if (entity.InitialChangeLogID == 0 && prop == first) { entity.InitialChangeLogID = changeLogEntry.Entry_ID; } else if (entity.EndingChangeLogID == 0 && prop == last) { entity.EndingChangeLogID = changeLogEntry.Entry_ID; } QueueChangeLogEntry(changeLogEntry); } } } else if (operation == OperationType.Update) { var comparison = CheckAlreadyQueued(entry); if (comparison == null) { var dbset = ctx.Set(type); comparison = dbset.Find(GetKeyValues(GetKeyNames(type), entry).Select(kv => kv.Value).ToArray()); } var changelist = GetAllChanges_ThisEntity(entry); foreach (var prop in props) { object compOldValue = null; if (comparison != null) { compOldValue = prop.GetValue(comparison); } if (comparison != null && prop.GetValue(entry) != null && prop.GetValue(comparison) != null && prop.GetValue(entry).ToString() != prop.GetValue(comparison).ToString()) { var changeLogEntry = new ChangeLog_Entry { FQAType = entry.GetType().AssemblyQualifiedName, TableUpdated = tableName, PrimaryKey = pk.Value != null?pk.Value.ToString() : null, ColumnUpdated = Repository_Helper.GetColumnName(type, ctx, prop.Name), NewValue = prop.GetValue(entry) == null ? null : prop.GetValue(entry).ToString(), OldValue = compOldValue == null ? null : compOldValue.ToString(), Operation = "Update", CommittedAt = null, ErrorAt = null, ErrorNotes = null }; if (entity.InitialChangeLogID == 0 && prop == first) { entity.InitialChangeLogID = changeLogEntry.Entry_ID; } else if (entity.EndingChangeLogID == 0 && prop == last) { entity.EndingChangeLogID = changeLogEntry.Entry_ID; } QueueChangeLogEntry(changeLogEntry); } else if (comparison != null) { //var col = Repository_Helper.GetColumnName(type, ctx, prop.Name); var lastChangeMadeThisColumn = changelist.Where(c => c.ColumnUpdated == prop.Name).OrderByDescending(c => c.Entry_ID).FirstOrDefault(); if (lastChangeMadeThisColumn == null) { if (entity.EndingChangeLogID == 0 && prop == last) { entity.EndingChangeLogID = ChangeLog_Entry.GetID_Counter() - 1; } continue; } if (prop.GetValue(entry) == null || lastChangeMadeThisColumn.NewValue != prop.GetValue(entry).ToString()) { var changeLogEntry = new ChangeLog_Entry { FQAType = entry.GetType().AssemblyQualifiedName, TableUpdated = tableName, PrimaryKey = pk.Value != null?pk.Value.ToString() : null, ColumnUpdated = Repository_Helper.GetColumnName(type, ctx, prop.Name), NewValue = prop.GetValue(entry) == null ? null : prop.GetValue(entry).ToString(), OldValue = lastChangeMadeThisColumn.NewValue, Operation = "Update", CommittedAt = null, ErrorAt = null, ErrorNotes = null }; if (entity.InitialChangeLogID == 0 && prop == first) { entity.InitialChangeLogID = changeLogEntry.Entry_ID; } else if (entity.EndingChangeLogID == 0 && prop == last) { entity.EndingChangeLogID = changeLogEntry.Entry_ID; } QueueChangeLogEntry(changeLogEntry); } else if (prop.GetValue(entry) == null) { if (entity.EndingChangeLogID == 0 && prop == last) { entity.EndingChangeLogID = ChangeLog_Entry.GetID_Counter() - 1; } continue; } } } } ContextChangesQueue.Add(entity); }
internal void QueueChangeLogEntry(ChangeLog_Entry cl) { _ChangeLogQueue.Add(cl); }