This is something I have used in the past to compare two entities, regardless of database, as long as they have the same column names and table names.
It returns the columns with values that are different (i.e. old vs. new values)
Note: plenty of examples of this on the internet, with different variation, etc.
public static List<PropertyInfo> CompareTo<T>(this T entityA, T entityB ) where T : class
{
var propertyInfoDifferences = new List<PropertyInfo>();
if(entityA == null || entityB == null) return propertyInfoDifferences;
if (entityA.GetType().Name != entityB.GetType().Name) return propertyInfoDifferences;
var properties = entityA.GetType().GetProperties();
foreach (var propertyInfo in properties)
{
if (propertyInfo == null) continue;
var entityAProperty = entityA.GetType().GetProperty(propertyInfo.Name);
var entityBProperty = entityB.GetType().GetProperty(propertyInfo.Name);
if(entityAProperty == null || entityBProperty == null) continue;
var entityAPropertyValue = entityAProperty.GetValue(entityA);
var entityBPropertyValue = entityBProperty.GetValue(entityB);
if (entityAPropertyValue == null && entityBPropertyValue == null) continue;
string entityAStringValue = null;
string entityBStringValue = null;
if (entityAPropertyValue != null)
{
entityAStringValue = entityAPropertyValue.ToString();
}
if (entityBPropertyValue != null)
{
entityBStringValue = entityBPropertyValue.ToString();
}
if (entityAStringValue != entityBStringValue)
{
propertyInfoDifferences.Add(propertyInfo);
}
}
return propertyInfoDifferences;
}
Implementation could be something like this:
List<PropertyInfo> propertyDiffs = entityA.CompareTo(entityB);