lance
lance

Reputation: 16342

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?

Upvotes: 2

Views: 1351

Answers (1)

Mark Oreta
Mark Oreta

Reputation: 10416

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();
}

Upvotes: 3

Related Questions