C#audit覆盖SaveChanges()方法.如何从表中查找实体的外键及其值

Mik*_*ike 6 .net c# asp.net-mvc overriding entity-framework

我重写了SaveChange()方法.我想要的是用简单的文本记录对实体所做的所有更改,例如"abc updated Name john to doe,..."等我已经实现了功能但是当实体中有一个外键要更新时像country_Id指出国家它生成的文本像"abc updated Country_Id 1 to 3,..."这就是我不想要它应该是这样的"abc更新国家加拿大到澳大利亚,......"

因此,要实现这一点,它应该知道外键及其值

我的代码是:

 public override int SaveChanges()
    {
        List<string> listChanges = new List<string>();
        List<string> listTable = new List<string>();

        var objectStateManager = ((IObjectContextAdapter)this).ObjectContext.ObjectStateManager;
        IEnumerable<ObjectStateEntry> changes =
            objectStateManager.GetObjectStateEntries(EntityState.Modified | EntityState.Added | EntityState.Deleted);


        foreach (ObjectStateEntry stateEntryEntity in changes)
        {

            var modifiedProperties = stateEntryEntity.GetModifiedProperties();
            foreach (var propName in modifiedProperties)
            {
                if (Convert.ToString(stateEntryEntity.OriginalValues[propName]) != Convert.ToString(stateEntryEntity.CurrentValues[propName]))
                {
                    listTable.Add(stateEntryEntity.EntityKey.EntitySetName);
                    listChanges.Add(propName + " From " + Convert.ToString(stateEntryEntity.OriginalValues[propName]) + " to " + Convert.ToString(stateEntryEntity.CurrentValues[propName]));

                }


                //System.Console.WriteLine("Property {0} changed from {1} to {2}",
                //     propName,
                //     stateEntryEntity.OriginalValues[propName],
                //     stateEntryEntity.CurrentValues[propName]);
            }
        }
         return base.SaveChanges();
    }
Run Code Online (Sandbox Code Playgroud)

orh*_*ndi 1

有一个功能可以实现此目的,我使用 Changetracker 来跟踪更改和日志。您可以在处理上下文之前获得所有更改,因此,您可以简单地像这样构建;

public override int SaveChanges()
        {
            foreach (var history in ChangeTracker.Entries()
                .Where(e => e.Entity is IModificationHistory && (e.State == EntityState.Added ||
                                                                 e.State == EntityState.Modified))
                .Select(e => e.Entity as IModificationHistory)
            )
            {
                history.DateModified = DateTime.Now;
                if (history.DateCreated == DateTime.MinValue)
                    history.DateCreated = DateTime.Now;
            }
            var result = base.SaveChanges();
            foreach (var history in ChangeTracker.Entries()
                .Where(e => e.Entity is IModificationHistory)
                .Select(e => e.Entity as IModificationHistory)
            )
                history.IsDirty = false;
            return result;
        }
Run Code Online (Sandbox Code Playgroud)

您也可以在 base.savechanges() 之前指定您的条目

这是我的历史界面

 public interface IModificationHistory
    {
        DateTime DateModified { get; set; }
        DateTime DateCreated { get; set; }
        bool IsDirty { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)