public static DB.SYS_Log New <T>(String fieldName, String tableName, Int64 entityId, T oldValue, T newValue) { SYS_Log returnValue = new SYS_Log(); returnValue.FieldName = fieldName; returnValue.TableName = tableName; returnValue.EntityId = entityId; returnValue.OldValue = oldValue.ToString(); returnValue.NewValue = newValue.ToString(); return(returnValue); }
internal static String Save(DB.SYS_Log entry, DataContext dataContext) { try { if (dataContext.EntitySystemContext.GetEntityState(entry) == System.Data.Entity.EntityState.Detached) { dataContext.EntitySystemContext.SYS_Log.Add(entry); } } catch (System.Data.Entity.Validation.DbEntityValidationException ex) { return(dataContext.PackageValidationException()); } return("Success"); }
private void LogChanges(System.Data.Entity.Infrastructure.DbChangeTracker dbChangeTracker) { foreach (System.Data.Entity.Infrastructure.DbEntityEntry entity in dbChangeTracker.Entries().Where(obj => { return(obj.State == System.Data.Entity.EntityState.Modified || obj.State == System.Data.Entity.EntityState.Deleted); })) { if (BL.ApplicationDataContext.NonProxyType(entity.GetType()) == typeof(DB.SYS_Log)) { continue; } //LogObject logObject = new LogObject(); //logObject.EntityType = BL.ApplicationDataContext.NonProxyType(entity.Entity.GetType()); //if (entity.State == System.Data.Entity.EntityState.Added) // logObject.Action = System.Data.Entity.EntityState.Added.ToString(); //else if (entity.State == System.Data.Entity.EntityState.Modified) // logObject.Action = System.Data.Entity.EntityState.Modified.ToString(); //else if (entity.State == System.Data.Entity.EntityState.Deleted) // logObject.Action = System.Data.Entity.EntityState.Deleted.ToString(); switch (entity.State) { case System.Data.Entity.EntityState.Modified: foreach (string propertyName in entity.CurrentValues.PropertyNames) { DB.SYS_Log sysLog = BL.SYS.SYS_Log.New; sysLog.EntityId = Convert.ToInt64(entity.CurrentValues["Id"]); sysLog.TableName = BL.ApplicationDataContext.NonProxyType(entity.Entity.GetType()).Name; if (propertyName == "CreatedOn" || propertyName == "CreatedBy" || propertyName == "Title") { continue; } sysLog.FieldName = propertyName; if (entity.CurrentValues[propertyName] == null && entity.OriginalValues[propertyName] == null) { } else if (entity.CurrentValues[propertyName] != null && entity.OriginalValues[propertyName] == null) { sysLog.OldValue = ""; sysLog.NewValue = entity.CurrentValues[propertyName].ToString(); EntityController.SaveSYS_Log(sysLog, this); } else if (entity.CurrentValues[propertyName] == null && entity.OriginalValues[propertyName] != null) { sysLog.OldValue = entity.OriginalValues[propertyName].ToString(); sysLog.NewValue = ""; EntityController.SaveSYS_Log(sysLog, this); } else if (entity.CurrentValues[propertyName].ToString() != entity.OriginalValues[propertyName].ToString()) { sysLog.OldValue = entity.OriginalValues[propertyName].ToString(); sysLog.NewValue = entity.CurrentValues[propertyName].ToString(); EntityController.SaveSYS_Log(sysLog, this); } } break; case System.Data.Entity.EntityState.Deleted: //if (BL.ApplicationDataContext.NonProxyType(entity.Entity.GetType()) != typeof(DB.ITM_InventorySupplier)) // throw new Exception("Deleting Entries not allowed in this system implement and archive"); break; } //if (entity.State == System.Data.Entity.EntityState.Added || entity.State == System.Data.Entity.EntityState.Modified) //{ // System.Xml.Linq.XDocument currentValues = new System.Xml.Linq.XDocument(new System.Xml.Linq.XElement(logObject.EntityType.Name)); // foreach (string propertyName in entity.CurrentValues.PropertyNames) // { // currentValues.Root.Add(new System.Xml.Linq.XElement(propertyName, entity.CurrentValues[propertyName])); // } // logObject.NewData = System.Text.RegularExpressions.Regex.Replace(currentValues.ToString(), @"\r\n+", " "); //} //if (entity.State == System.Data.Entity.EntityState.Modified || entity.State == System.Data.Entity.EntityState.Deleted) //{ // System.Xml.Linq.XDocument originalValues = new System.Xml.Linq.XDocument(new System.Xml.Linq.XElement(logObject.EntityType.Name)); // foreach (string propertyName in entity.OriginalValues.PropertyNames) // { // originalValues.Root.Add(new System.Xml.Linq.XElement(propertyName, entity.OriginalValues[propertyName])); // } // logObject.OldData = System.Text.RegularExpressions.Regex.Replace(originalValues.ToString(), @"\r\n+", " "); //} //auditTrailList.Add(auditObject); } }