How to translate ObjectStateEntry.OriginalValues ​​to Entity?

How to translate ObjectStateEntry.OriginalValues ​​to Entity? I thought I could do this using the ObjectContext.Translate method, but instead I need to pass a DataReader. Are there any ways?

+4
source share
1 answer

To convert OriginalValues to an Entity object, you can use reflection over properties (you have names buried in OriginalValues.DataRecordInfo.FieldMetadata[i].FieldType.Name ). What do you plan to do later? Compare properties of objects by properties? It would be easier to get a list of changed 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 entity objects and register 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()) ); } } 
+3
source

All Articles