0

我在下面的代码中遇到问题。一切都适用于获取添加和删除的条目。我可以访问我修改部分中的所有新数据,但由于某种原因我无法获得原始值。任何帮助将不胜感激

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();



    }
4

2 回答 2

1

看看这篇文章,作者提到他的第一种方法在插入时失败了,他做了一些修改。

于 2013-01-15T07:17:32.970 回答
0

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

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

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

于 2013-12-21T14:54:35.543 回答