entity-framework-4saveobjectcontextauditingtimestamping

ObjectContext's SavingChanges: find out which columns have changed?


I'm handling ObjectContext's SavingChanges event to timestamp entries. The requirement is that, if only ColumnA has changed, I don't timestamp the entry when it changes.

Is there a way that I can find out which columns have changed (are changing) during this event?


Solution

  • This should work for you, this will loop through any Added/Modified entries, and if there is more than 1 modified property, and it's not "ColumnA" it you can modify the timestamp:

    public int SaveChanges()
    {
         foreach( ObjectStateEntry entry in ObjectStateManager.GetObjectStateEntries( EntityState.Added | EntityState.Modified ) )
         {
             var properties = entry.GetModifiedProperties();
    
              if (!(properties.Count() == 1 && properties.First() == "ColumnA"))
              {
                  //modify timestamp here
              }
         }
    
        return base.SaveChanges();
    }