I override the SaveChange () method. I want to log all changes made by an entity to the database in plain text, for example, "abc updated Name john to doe, ..." etc. I got the functionality, but when there is a foreign key in the entity that will be updated, like country_Id, which points to the Country table, it generates text, for example "abc updated Country_Id 1 to 3, ..." that I do not want it be like "abc updated Country Canada to Australia, ..."
To achieve this, he must know the foreign keys and their meaning
My code is:
public override int SaveChanges() { List<string> listChanges = new List<string>(); List<string> listTable = new List<string>(); var objectStateManager = ((IObjectContextAdapter)this).ObjectContext.ObjectStateManager; IEnumerable<ObjectStateEntry> changes = objectStateManager.GetObjectStateEntries(EntityState.Modified | EntityState.Added | EntityState.Deleted); foreach (ObjectStateEntry stateEntryEntity in changes) { var modifiedProperties = stateEntryEntity.GetModifiedProperties(); foreach (var propName in modifiedProperties) { if (Convert.ToString(stateEntryEntity.OriginalValues[propName]) != Convert.ToString(stateEntryEntity.CurrentValues[propName])) { listTable.Add(stateEntryEntity.EntityKey.EntitySetName); listChanges.Add(propName + " From " + Convert.ToString(stateEntryEntity.OriginalValues[propName]) + " to " + Convert.ToString(stateEntryEntity.CurrentValues[propName])); }
source share