diff --git a/src/EFCore/ChangeTracking/Internal/SnapshotFactoryFactory.cs b/src/EFCore/ChangeTracking/Internal/SnapshotFactoryFactory.cs index ab37b8d061d..f133466dac9 100644 --- a/src/EFCore/ChangeTracking/Internal/SnapshotFactoryFactory.cs +++ b/src/EFCore/ChangeTracking/Internal/SnapshotFactoryFactory.cs @@ -192,6 +192,11 @@ private Expression CreateSnapshotValueExpression(Expression expression, IPropert expression, comparer.SnapshotExpression.Body); + if (snapshotExpression.Type != propertyBase.ClrType) + { + snapshotExpression = Expression.Convert(snapshotExpression, propertyBase.ClrType); + } + expression = propertyBase.ClrType.IsNullableType() ? Expression.Condition( Expression.Equal(expression, Expression.Constant(null, propertyBase.ClrType)), diff --git a/test/EFCore.Specification.Tests/CustomConvertersTestBase.cs b/test/EFCore.Specification.Tests/CustomConvertersTestBase.cs index f450c1891e5..f1aef2d11ab 100644 --- a/test/EFCore.Specification.Tests/CustomConvertersTestBase.cs +++ b/test/EFCore.Specification.Tests/CustomConvertersTestBase.cs @@ -1183,7 +1183,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con new ValueComparer>( (v1, v2) => v1.SequenceEqual(v2), v => v.GetHashCode(), - v => (IDictionary)new Dictionary(v))); + v => new Dictionary(v))); }); var urlConverter = new UrlSchemeRemover(); diff --git a/test/EFCore.Specification.Tests/ValueConvertersEndToEndTestBase.cs b/test/EFCore.Specification.Tests/ValueConvertersEndToEndTestBase.cs index d2823a14dc2..d9ba187927c 100644 --- a/test/EFCore.Specification.Tests/ValueConvertersEndToEndTestBase.cs +++ b/test/EFCore.Specification.Tests/ValueConvertersEndToEndTestBase.cs @@ -918,7 +918,7 @@ public EnumerableOfIntComparer() : base( (c1, c2) => (c1 == null && c2 == null) || (c1 != null && c2 != null && c1.SequenceEqual(c2)), c => c == null ? 0 : c.Aggregate(0, (a, v) => HashCode.Combine(a, v.GetHashCode())), - c => c == null ? null : (IEnumerable)c.ToList()) + c => c == null ? null : c.ToList()) { } }