private void StartAudit(ObjectStateEntry entry) { //object target = CloneEntity((EntityObject)entry.Entity); var auditTrail = new AuditRecord(); try { var b = SessionHelper.FetchEmail(System.Web.HttpContext.Current.Session); if (string.IsNullOrEmpty(b)) { auditTrail.UserName = ""; } else { auditTrail.UserName = b.Split('@')[0];//HttpContext.Current.Session["FullName"].ToString(); } } catch (NullReferenceException) { auditTrail.UserName = ""; } auditTrail.TableName = entry.EntitySet.Name; auditTrail.AuditDate = DateTime.Now; //foreach (string propName in entry.GetModifiedProperties()) //{ var detail = LoadEntiyProperty(entry, auditTrail); this.AuditRecords.Add(detail); //this.AddToAuditTrails(detail); //} }
private AuditRecord LoadEntiyProperty(ObjectStateEntry entry, AuditRecord auditTrail) { // var auditTrailDetail = new AuditTrailDetail(); if (entry.State == EntityState.Added) { //entry is Added // continue and get property list var currentValues = entry.CurrentValues; for (var i = 0; i < currentValues.FieldCount; i++) { IsCoreType(currentValues[i].GetType()); { var auditTrailDetail = new AuditRecordField(); auditTrailDetail.MemberName = currentValues.DataRecordInfo.FieldMetadata[i].FieldType.Name; auditTrailDetail.NewValue = (currentValues[i].ToString()); auditTrail.AuditRecordFields.Add(auditTrailDetail); } } auditTrail.Action = ChangeAction.Insert.ToString(); } if (entry.State == EntityState.Modified) { //entry is deleted var currentValues = entry.CurrentValues; var editedvalues = entry.OriginalValues; for (var i = 0; i < currentValues.FieldCount; i++) { var auditTrailDetail = new AuditRecordField(); IsCoreType(currentValues[i].GetType()); { auditTrailDetail.MemberName = currentValues.DataRecordInfo.FieldMetadata[i].FieldType.Name; auditTrailDetail.NewValue = (currentValues[i].ToString()); //auditTrail.AuditTrailDetails.Add(auditTrailDetail); } IsCoreType(editedvalues[i].GetType()); { auditTrailDetail.OldValue = (editedvalues[i].ToString()); } auditTrail.AuditRecordFields.Add(auditTrailDetail); } auditTrail.Action = ChangeAction.Update.ToString(); } if (entry.State == EntityState.Deleted) { //entry is modified var deletedvalues = entry.OriginalValues; for (var i = 0; i < deletedvalues.FieldCount; i++) { IsCoreType(deletedvalues[i].GetType()); { var auditTrailDetail = new AuditRecordField(); auditTrailDetail.MemberName = deletedvalues.GetName(i);//.FieldMetadata[i].FieldType.Name; auditTrailDetail.OldValue = deletedvalues.GetValue(i).ToString(); auditTrail.AuditRecordFields.Add(auditTrailDetail); } } auditTrail.Action = ChangeAction.Delete.ToString(); } //auditTrail.AuditTrailDetails.Add(auditTrailDetail); return(auditTrail); //} }