title: Auditing in Entity Framework
body: <p>After going through Entity Framework I have a couple of questions on implementing auditing in Entity Framework.</p>
  
  <p>I want to store each column values that is created or updated to a different audit table.</p>
  
  <ol>
  <li><p>Right now I am calling SaveChanges(false) to save the records in the DB(still the changes in context is not reset). Then get the added | modified records and loop through the GetObjectStateEntries. But don't know how to get the values of the columns where their values are filled by stored proc. ie, createdate, modifieddate etc.</p></li>
  <li><p>Below is the sample code I am working on it.</p>
  
  <pre><code>// Get the changed entires( ie, records)
  IEnumerable&lt;ObjectStateEntry&gt; changes = context.ObjectStateManager.GetObjectStateEntries(EntityState.Modified);
  
  // Iterate each ObjectStateEntry( for each record in the update/modified collection)
  foreach (ObjectStateEntry entry in changes)
  {
      // Iterate the columns in each record and get thier old and new value respectively
      foreach (var columnName in entry.GetModifiedProperties())
      {
          string oldValue = entry.OriginalValues[columnName].ToString();
          string newValue = entry.CurrentValues[columnName].ToString();
  
          // Do Some Auditing by sending entityname, columnname, oldvalue, newvalue
      }
  }
  
  changes = context.ObjectStateManager.GetObjectStateEntries(EntityState.Added);
  
  foreach (ObjectStateEntry entry in changes)
  {
      if (entry.IsRelationship) continue;
      var columnNames = (from p in entry.EntitySet.ElementType.Members
                         select p.Name).ToList();
  
      foreach (var columnName in columnNames)
      {
          string newValue = entry.CurrentValues[columnName].ToString();
  
          // Do Some Auditing by sending entityname, columnname, value
      }
  }
  </code></pre></li>
  </ol>