views:

54

answers:

1

How to translate ObjectStateEntry.OriginalValues into Entity? I thought I can do this with ObjectContext.Translate method but I have to pass DataReader instead. Is there any ways?

+1  A: 

In order to convert OriginalValues to an Entity object you can use reflection over the properties (you have the names buried in OriginalValues.DataRecordInfo.FieldMetadata[i].FieldType.Name). What are you planning to do afterwards, however? Compare the entities property-by-property? It would be easier to get the list of modified properties directly:

var originalValues = yourEntry.OriginalValues;
var currentValues = yourEntry.CurrentValues;
var modifiedProperties = yourEntry.GetModifiedProperties();

foreach (var modifiedProperty in modifiedProperties)
{
    LogChange(string.Format("Property: {0}, Original value: {1}, Current value: {2}", 
                            modifiedProperty,
                            originalValues[modifiedProperty],
                            currentValues[modifiedProperty])
    );
}

Alternatively, you can implement the OnSomePropertyChanging methods for your entity objects and log the changes in real time:

public partial class YourEntity
{
    partial void OnSomePropertyChanging(string value)
    {
        LogChange(String.Format("Property: SomeProperty, Original value: {0}, Current value: {1}, Change time: {2}", 
                  this.SomeProperty,
                  value,
                  DateTime.Now.ToShortTimeString())
        );
    }
}
Yakimych