Get list of changed properties on EF 4.2 Code Only

777 Views Asked by At

(sorry for my bad english)

Hey, I need to log all changes that happens on my database for auditing purposes. When I insert or delete a entity, it is easy but when I update something, I need to save what properties have changed and from/to what values. My domain service is a DbDomainService. How can I get the changes? Do I need to hit the database to see the old values and compare it myself?

1

There are 1 best solutions below

1
codeputer On

There is more elegant means of doing this (e.g. Using Aspect coding techniques), but I had one isolated case in which I did this. This is the standard update for WCF RIA Services, on the server side, for each entity that you allow updates to occur.

public void UpdatePackingSlip(PackingSlip currentPackingSlip)
{

  var BeforeUpdate_PackingSlip = this.ChangeSet.GetOriginal(currentPackingSlip);
  //at this point you can compare the original values from the updated values, and capture
  // whatever you want todo

Just make sure you don't change the code that attaches the object back to the object context.