DbPropertyEntryExtensions.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System.Data.Entity;
  2. using System.Data.Entity.Infrastructure;
  3. namespace IwbZero.EntityHistory.Extensions
  4. {
  5. internal static class DbPropertyEntryExtensions
  6. {
  7. internal static object GetNewValue(this DbPropertyEntry propertyEntry)
  8. {
  9. if (propertyEntry.EntityEntry.State == EntityState.Deleted)
  10. {
  11. return propertyEntry.OriginalValue;
  12. }
  13. return propertyEntry.CurrentValue;
  14. }
  15. internal static object GetOriginalValue(this DbPropertyEntry propertyEntry)
  16. {
  17. if (propertyEntry.EntityEntry.State == EntityState.Added)
  18. {
  19. return propertyEntry.CurrentValue;
  20. }
  21. return propertyEntry.OriginalValue;
  22. }
  23. internal static bool HasChanged(this DbPropertyEntry propertyEntry)
  24. {
  25. if (propertyEntry.EntityEntry.State == EntityState.Added)
  26. {
  27. return propertyEntry.CurrentValue != null;
  28. }
  29. if (propertyEntry.EntityEntry.State == EntityState.Deleted)
  30. {
  31. return propertyEntry.OriginalValue != null;
  32. }
  33. return !(propertyEntry.OriginalValue?.Equals(propertyEntry.CurrentValue) ?? propertyEntry.CurrentValue == null);
  34. }
  35. }
  36. }