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