DbComplexPropertyEntryExtensions.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System.Data.Entity.Core.Metadata.Edm;
  2. using System.Data.Entity.Infrastructure;
  3. namespace IwbZero.EntityHistory.Extensions
  4. {
  5. internal static class DbComplexPropertyEntryExtensions
  6. {
  7. internal static bool HasChanged(this DbComplexPropertyEntry propertyEntry, EdmProperty complexProperty)
  8. {
  9. return HasModifications(complexProperty, propertyEntry.GetNewValue(), propertyEntry.GetOriginalValue());
  10. }
  11. private static bool HasModifications(EdmProperty complexTypeProperty, object newValue, object originalValue)
  12. {
  13. if (!complexTypeProperty.IsComplexType)
  14. {
  15. return false;
  16. }
  17. if (newValue == null && originalValue == null)
  18. {
  19. return false;
  20. }
  21. var isModified = false;
  22. foreach (var property in complexTypeProperty.ComplexType.Properties)
  23. {
  24. var propertyNewValue = newValue?.GetType()?.GetProperty(property.Name)?.GetValue(newValue);
  25. var propertyOldValue = originalValue?.GetType()?.GetProperty(property.Name)?.GetValue(originalValue);
  26. if (property.IsPrimitiveType)
  27. {
  28. isModified = !(propertyNewValue?.Equals(propertyOldValue)) ?? false;
  29. }
  30. else if (property.IsComplexType)
  31. {
  32. isModified = HasModifications(property, propertyNewValue, propertyOldValue);
  33. }
  34. if (isModified)
  35. {
  36. break;
  37. }
  38. }
  39. return isModified;
  40. }
  41. }
  42. }