From a3531e9a3e8fe9de7b04042099511df1696da4e6 Mon Sep 17 00:00:00 2001 From: Colin Dawson Date: Mon, 2 Mar 2026 22:41:53 +0000 Subject: [PATCH] Upgraded audit engine to support EFCore's has conversion implemenation --- .../AuditEngine/AuditEngineCore.cs | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/e-suite.Database.Audit/e-suite.Database.Audit/AuditEngine/AuditEngineCore.cs b/e-suite.Database.Audit/e-suite.Database.Audit/AuditEngine/AuditEngineCore.cs index bcc796f..2b1e095 100644 --- a/e-suite.Database.Audit/e-suite.Database.Audit/AuditEngine/AuditEngineCore.cs +++ b/e-suite.Database.Audit/e-suite.Database.Audit/AuditEngine/AuditEngineCore.cs @@ -69,9 +69,18 @@ public class AuditEngineCore continue; } - var currentValue = property.CurrentValue == null ? null : Convert.ChangeType(property.CurrentValue, property.CurrentValue!.GetType()); - var oldValue = property.OriginalValue == null ? null : Convert.ChangeType(property.OriginalValue, property.OriginalValue!.GetType()); + // Value converter + comparer + var converter = property.Metadata.GetValueConverter(); //todo need a unit test for when a value converter is present on the table. + var comparer = property.Metadata.GetValueComparer(); + // Raw CLR values + var currentClr = property.CurrentValue; + var oldClr = property.OriginalValue; + + // Convert to provider (JSON for Tasks) + var currentValue = converter?.ConvertToProvider(currentClr) ?? currentClr; + var oldValue = converter?.ConvertToProvider(oldClr) ?? oldClr; + var isSoftDelete = propertyInfo.CustomAttributes.SingleOrDefault(x => x.AttributeType == typeof(AuditSoftDeleteAttribute)); @@ -96,9 +105,11 @@ public class AuditEngineCore var currentDisplayName = GetNewName(property) ?? GetEnumAsString(currentValue); var oldDisplayName = GetOldName(property) ?? GetEnumAsString(oldValue); - var valuesMatch = (currentValue == null && oldValue == null) || - (currentValue != null && currentValue.Equals(oldValue)); - + // Compare using EF’s own comparer if available + var valuesMatch = comparer != null + ? comparer.Equals(currentClr, oldClr) + : Equals(currentValue, oldValue); + if (isRedacted) { const string redacted = "";