以下のコードに問題があります。追加および削除されたエントリを取得するには、すべて正常に機能します。変更したセクションのすべての新しいデータにアクセスできますが、何らかの理由で元の値を取得できません。どんな助けでも大歓迎です
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();
}