实体框架审计跟踪

时间:2012-11-26 19:18:10

标签: c# frameworks entity

我的代码问题如下。一切正常,可以获得添加和删除的条目。我可以访问修改后的部分中的所有新数据,但由于某种原因,我无法获得原始值。任何帮助将非常感谢

public override int SaveChanges()
    {

        ChangeTracker.DetectChanges(); // Important!

        ObjectContext ctx = ((IObjectContextAdapter)this).ObjectContext;

        List<ObjectStateEntry> objectStateEntryList =
            ctx.ObjectStateManager.GetObjectStateEntries(EntityState.Added
                                                       | EntityState.Modified
                                                       | EntityState.Deleted)
            .ToList();
        List<Log> LogEntries = new List<Log>();

        foreach (ObjectStateEntry entry in objectStateEntryList)
        {

            if ((!entry.IsRelationship) && (entry.Entity is Scan))
            {
                Log logEntry = new Log();
                JavaScriptSerializer serializer= new JavaScriptSerializer();
                logEntry.TimeStamp = System.DateTime.Now;
                logEntry.Username = "me";
                IEnumerable<string> modifiedProperties = entry.GetModifiedProperties();
                logEntry.ChangedColumns = serializer.Serialize(modifiedProperties);
                logEntry.TableName = entry.EntitySet.Name;

                switch (entry.State)
                {
                    case EntityState.Added:
                        {

                            logEntry.Action = "Added";
                            logEntry.NewValue = serializer.Serialize(entry.Entity);
                            logEntry.OriginalValue = "";

                            // write log...
                            break;
                        }
                    case EntityState.Deleted:
                        {// write log...

                            logEntry.Action = "Deleted";
                            logEntry.NewValue = serializer.Serialize(entry.Entity);
                            logEntry.OriginalValue = "";

                            break;
                        }
                    case EntityState.Modified:
                        {
                            logEntry.NewValue = "{";
                            logEntry.OriginalValue = "{";
                            foreach (string propertyName in
                                         entry.GetModifiedProperties())
                            {
                                DbDataRecord original = entry.OriginalValues;
                                string oldValue = original.GetValue(
                                    original.GetOrdinal(propertyName))
                                    .ToString();

                                CurrentValueRecord current = entry.CurrentValues;
                                string newValue = current.GetValue(
                                    current.GetOrdinal(propertyName))
                                    .ToString();
                                logEntry.NewValue += "\"" + propertyName + "\":\"" + newValue + "\",";
                                logEntry.OriginalValue += "\"" + propertyName + "\":\"" + oldValue + "\",";
                            }
                            logEntry.NewValue = logEntry.NewValue.TrimEnd(',') + "}";
                            logEntry.OriginalValue = logEntry.OriginalValue.TrimEnd(',') + "}";
                            break;
                        }
                }
                LogEntries.Add(logEntry);

            }
        }
        foreach (Log addLog in LogEntries)
        {
            this.Logs.Add(addLog);
        }
        return base.SaveChanges();



    }

2 个答案:

答案 0 :(得分:1)

查看此article,作者提到他的第一个approach插入失败,他对此进行了一些修改。

答案 1 :(得分:0)

您可能想要查看Codeplex上的AuditDBContext项目。 http://auditdbcontext.codeplex.com/

作者存储更新或删除新值以及旧值与其他详细信息一起保存,例如提交操作的用户,时间戳等。

此项目的源代码将向您展示如何获取旧值。