Logging every data change with Entity Framework

后端 未结 8 1036
醉酒成梦
醉酒成梦 2020-12-02 14:43

There is a need from a customer to log every data change to a logging table with the actual user who made the modification. The application is using one SQL user to access t

8条回答
  •  庸人自扰
    2020-12-02 15:10

    This is what I used found here I modified it because it didn't work

    private object GetPrimaryKeyValue(DbEntityEntry entry)
            {
                var objectStateEntry = ((IObjectContextAdapter)this).ObjectContext.ObjectStateManager.GetObjectStateEntry(entry.Entity);
                object o = objectStateEntry.EntityKey.EntityKeyValues[0].Value;
                return o;
            }
    
             private bool inExcludeList(string prop)
            {
                string[] excludeList = { "props", "to", "exclude" };
                return excludeList.Any(s => s.Equals(prop));
            }
    
            public int SaveChanges(User user, string UserId)
            {
                var modifiedEntities = ChangeTracker.Entries()
                    .Where(p => p.State == EntityState.Modified).ToList();
                var now = DateTime.Now;
    
                foreach (var change in modifiedEntities)
                {
    
                    var entityName = ObjectContext.GetObjectType(change.Entity.GetType()).Name;
                    var primaryKey = GetPrimaryKeyValue(change);
                    var DatabaseValues = change.GetDatabaseValues();
    
                    foreach (var prop in change.OriginalValues.PropertyNames)
                    {
                        if(inExcludeList(prop))
                        {
                            continue;
                        }
    
                        string originalValue = DatabaseValues.GetValue(prop)?.ToString();
                        string currentValue = change.CurrentValues[prop]?.ToString();
    
                        if (originalValue != currentValue)
                        {
                            ChangeLog log = new ChangeLog()
                            {
                                EntityName = entityName,
                                PrimaryKeyValue = primaryKey.ToString(),
                                PropertyName = prop,
                                OldValue = originalValue,
                                NewValue = currentValue,
                                ModifiedByName = user.LastName + ", " + user.FirstName,
                                ModifiedById = UserId,
                                ModifiedBy = user,
                                ModifiedDate = DateTime.Now
                            };
    
                            ChangeLogs.Add(log);
                        }
                    }
                }
                return base.SaveChanges();
            }
    
    
    
    public class ChangeLog 
        {
            public int Id { get; set; }
            public string EntityName { get; set; }
            public string PropertyName { get; set; }
            public string PrimaryKeyValue { get; set; }
            public string OldValue { get; set; }
            public string NewValue { get; set; }
            public string ModifiedByName { get; set; }
    
    
    
            [ForeignKey("ModifiedBy")]
            [DisplayName("Modified By")]
            public string ModifiedById { get; set; }
            public virtual User ModifiedBy { get; set; }
    
    
            [Column(TypeName = "datetime2")]
            public DateTime? ModifiedDate { get; set; }
        }
    
        

    提交回复
    热议问题