public static void MakeUpdateJournalEntry(IUser User, System.Data.Entity.Infrastructure.DbEntityEntry dbEntityEntry) { var entityType = System.Data.Entity.Core.Objects.ObjectContext.GetObjectType(dbEntityEntry.Entity.GetType()); var EntityName = entityType.Name; var OriginalObj = dbEntityEntry.GetDatabaseValues(); var CurrentObj = dbEntityEntry.CurrentValues; var userName = User != null ? User.Name : "AppAdmin"; string dispValue = ""; try { Type EntityType = dbEntityEntry.Entity.GetType(); dynamic EntityObj = Convert.ChangeType(dbEntityEntry.Entity, EntityType); dispValue = EntityObj.DisplayValue; } catch { } if (string.IsNullOrEmpty(dispValue)) { if (EntityName == "ApplicationUser") { dispValue = Convert.ToString(CurrentObj.GetValue <object>("UserName")); } if (EntityName == "IdentityRole") { dispValue = Convert.ToString(CurrentObj.GetValue <object>("Name")); } } foreach (var property in dbEntityEntry.OriginalValues.PropertyNames) { if (property == "DisplayValue" || property == "ConcurrencyKey" || property.ToLower().Contains("password") || property.ToLower().Contains("security")) { continue; } var original = OriginalObj.GetValue <object>(property); var current = CurrentObj.GetValue <object>(property); if (original != current && (original == null || !original.Equals(current))) { using (var db = new JournalEntryContext()) { JournalEntry Je = new JournalEntry(); Je.DateTimeOfEntry = DateTime.UtcNow; Je.EntityName = EntityName; Je.UserName = userName; Je.Type = dbEntityEntry.State.ToString(); var displayValue = dispValue; Je.RecordInfo = displayValue; Je.PropertyName = property; Je.OldValue = Convert.ToString(original); Je.NewValue = Convert.ToString(current); //Je.RecordId = Convert.ToInt64(id); db.JournalEntries.Add(Je); db.SaveChanges(); } } } }
public static IEnumerable <string> EnumeratePropertyDifferences <T>(this T obj1, T obj2, string state, string EntityName) { PropertyInfo[] properties = typeof(T).GetProperties().Where(p => p.PropertyType.FullName.StartsWith("System")).ToArray(); List <string> changes = new List <string>(); string id = Convert.ToString(typeof(T).GetProperty("Id").GetValue(obj2, null)); string dispValue = Convert.ToString(typeof(T).GetProperty("DisplayValue").GetValue(obj2, null)); foreach (PropertyInfo pi in properties) { if (pi.Name == "DisplayValue" || pi.Name == "ConcurrencyKey" || typeof(T).GetProperty(pi.Name).PropertyType.Name == "ICollection`1") { continue; } object value1 = typeof(T).GetProperty(pi.Name).GetValue(obj1, null); object value2 = typeof(T).GetProperty(pi.Name).GetValue(obj2, null); if (value1 != value2 && (value1 == null || !value1.Equals(value2))) { using (var db = new JournalEntryContext()) { JournalEntry Je = new JournalEntry(); Je.DateTimeOfEntry = DateTime.UtcNow; Je.EntityName = EntityName; Je.UserName = System.Web.HttpContext.Current.User.Identity.Name; Je.Type = state; var displayValue = dispValue; Je.RecordInfo = displayValue; Je.PropertyName = pi.Name; var assolist = ModelReflector.Entities.Where(p => p.Name == EntityName).ToList()[0].Associations.Where(p => p.AssociationProperty == pi.Name).ToList(); if (assolist.Count() > 0) { Je.PropertyName = assolist[0].DisplayName; if (value1 != null) { Je.OldValue = GetDisplayValueForAssociation(assolist[0].Target, Convert.ToString(value1)); } if (value2 != null) { Je.NewValue = GetDisplayValueForAssociation(assolist[0].Target, Convert.ToString(value2)); } } else { Je.OldValue = Convert.ToString(value1); Je.NewValue = Convert.ToString(value2); } Je.RecordId = Convert.ToInt64(id); db.JournalEntries.Add(Je); db.SaveChanges(); } } } return(changes); }
public static void AddJournalEntryCommon(IUser User, ApplicationDbContext identitydb, string role, string EntityName) { JournalEntryContext db = new JournalEntryContext(); JournalEntry Je = new JournalEntry(); Je.DateTimeOfEntry = DateTime.UtcNow; Je.EntityName = EntityName; Je.UserName = User.Name; Je.Type = "Added"; Je.RecordInfo = role; Je.RecordId = 0; db.JournalEntries.Add(Je); db.SaveChanges(); }
private void MakeAddJournalEntry(string id, string state, ObjectStateEntry entry) { JournalEntryContext db = new JournalEntryContext(); JournalEntry Je = new JournalEntry(); Je.DateTimeOfEntry = DateTime.UtcNow; Je.EntityName = "BusinessRule"; Je.UserName = System.Web.HttpContext.Current.User.Identity.Name; Je.Type = state; if (entry.Entity is BusinessRule) { var displayValue = ((GeneratorBase.MVC.Models.BusinessRule)(entry.Entity)).DisplayValue; Je.RecordInfo = displayValue; Je.RecordId = Convert.ToInt64(id); db.JournalEntries.Add(Je); db.SaveChanges(); } }
public static void MakeAddJournalEntry(IUser User, ApplicationDbContext identitydb, System.Data.Entity.Infrastructure.DbEntityEntry entry) { var entityType = System.Data.Entity.Core.Objects.ObjectContext.GetObjectType(entry.Entity.GetType()); var EntityName = entityType.Name; var userName = User != null ? User.Name : "AppAdmin"; JournalEntryContext db = new JournalEntryContext(); JournalEntry Je = new JournalEntry(); Je.DateTimeOfEntry = DateTime.UtcNow; Je.EntityName = EntityName; Je.UserName = userName; Je.Type = entry.State.ToString(); Je.RecordId = 0; Type EntityType = entry.Entity.GetType(); var displayValue = ""; dynamic EntityObj = Convert.ChangeType(entry.Entity, EntityType); try { displayValue = EntityObj.DisplayValue; } catch { } if (EntityName == "ApplicationUser") { displayValue = EntityObj.UserName; } if (EntityName == "IdentityUserRole") { var roleid = EntityObj.RoleId; var userid = EntityObj.UserId; displayValue = identitydb.Users.Find(userid).UserName + " - " + identitydb.Roles.Find(roleid).Name; } Je.RecordInfo = displayValue; db.JournalEntries.Add(Je); db.SaveChanges(); }