title: Entity Framework DbContext SaveChanges() OriginalValue Incorrect
body: <p>I am trying to implement an AuditLog using EF 4.1, by overriding the SaveChanges() method as discussed in the following places:</p>
  
  <ul>
  <li><a href="http://jmdority.wordpress.com/2011/07/20/using-entity-framework-4-1-dbcontext-change-tracking-for-audit-logging/" rel="nofollow noreferrer">http://jmdority.wordpress.com/2011/07/20/using-entity-framework-4-1-dbcontext-change-tracking-for-audit-logging/</a></li>
  <li><a href="https://stackoverflow.com/questions/6156818/entity-framework-4-1-dbcontext-override-savechanges-to-audit-property-change">Entity Framework 4.1 DbContext Override SaveChanges to Audit Property Change</a></li>
  </ul>
  
  <p>I am having problems with the "modified" entries though. Whenever I attempt to get at the <strong>OriginalValue</strong> of the property in question, it always has the same value as it does in the <strong>CurrentValue</strong> field.</p>
  
  <p>I first use this code, and it successfully identifies the Entries that are modified:</p>
  
  <pre><code>public int SaveChanges(string userID)
  {
  
      // Have tried both with and without the following line, and received same results:
      // ChangeTracker.DetectChanges();
  
      foreach (
        var ent in this.ChangeTracker
                       .Entries()
                       .Where( p =&gt; p.State == System.Data.EntityState.Added ||
                                       p.State == System.Data.EntityState.Deleted ||
                                       p.State == System.Data.EntityState.Modified ))
      {
          // For each change record, get the audit record entries and add them
          foreach (AuditLog log in GetAuditRecordsForChange(ent, userID))
          {
              this.AuditLog.Add(log);
          }
  
      }
  
      return base.SaveChanges();
  }
  </code></pre>
  
  <p>The problem is in this (abbreviated code):</p>
  
  <pre><code>    private List&lt;AuditLog&gt; GetAuditRecordsForChange(DbEntityEntry dbEntry, string userID)
      {
          if (dbEntry.State == System.Data.EntityState.Modified)
          {
              foreach (string propertyName in dbEntry.OriginalValues.PropertyNames)
              {
                  if (!object.Equals(dbEntry.OriginalValues.GetValue&lt;object&gt;(propertyName),
                      dbEntry.CurrentValues.GetValue&lt;object&gt;(propertyName)))
                  {
                          // It never makes it into this if block, even when
                          //    the property has been updated.
                  }
  
                  // If I updated the property "Name" which was originally "OldName" to the value "NewName" and then break here and inspect the values by calling:
                  //      ?dbEntry.OriginalValues.GetValue&lt;object&gt;("Name").ToString()
  
                  // the result will be "NewName" and not "OldName" as expected
               }
           }
      }
  </code></pre>
  
  <p>The strange thing is that the call to <code>dbEntry.Property(propertyName).IsModified();</code> will 
  return true in this case.  It is just that the OriginalValue doesn't have the expected value inside.  Would anyone be willing to help point me in the right direction?  I cannot seem to get this to work correctly.</p>