diff --git a/src/EFCore.Relational/Query/RelationalSqlTranslatingExpressionVisitor.StructuralEquality.cs b/src/EFCore.Relational/Query/RelationalSqlTranslatingExpressionVisitor.StructuralEquality.cs index 36b22d3f651..6b68a6ebf78 100644 --- a/src/EFCore.Relational/Query/RelationalSqlTranslatingExpressionVisitor.StructuralEquality.cs +++ b/src/EFCore.Relational/Query/RelationalSqlTranslatingExpressionVisitor.StructuralEquality.cs @@ -522,10 +522,20 @@ bool TryProcessJson(Expression expression, [NotNullWhen(true)] out SqlExpression } } - private bool TryTranslatePropertyAccess(Expression target, IPropertyBase property, [NotNullWhen(true)] out SqlExpression? translation) + private bool TryTranslatePropertyAccess(Expression target, IProperty property, [NotNullWhen(true)] out SqlExpression? translation) { var expression = CreatePropertyAccessExpression(target, property); - translation = Translate(expression); + + if (property.GetTypeMapping() is RelationalTypeMapping relationalTypeMapping) + { + translation = Translate(expression, applyDefaultTypeMapping: false); + translation = _sqlExpressionFactory.ApplyTypeMapping(translation, relationalTypeMapping); + } + else + { + translation = Translate(expression); + } + return translation is not null; } diff --git a/test/EFCore.Specification.Tests/Query/AdHocComplexTypeQueryTestBase.cs b/test/EFCore.Specification.Tests/Query/AdHocComplexTypeQueryTestBase.cs index 98d78740835..534effb606e 100644 --- a/test/EFCore.Specification.Tests/Query/AdHocComplexTypeQueryTestBase.cs +++ b/test/EFCore.Specification.Tests/Query/AdHocComplexTypeQueryTestBase.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.ComponentModel.DataAnnotations.Schema; + namespace Microsoft.EntityFrameworkCore.Query; // ReSharper disable ClassNeverInstantiated.Local @@ -175,6 +177,49 @@ public class AllOptionalsComplexType #endregion ShadowDiscriminator + #region 36837 + + [ConditionalFact] + public virtual async Task Complex_type_equality_with_non_default_type_mapping() + { + var contextFactory = await InitializeAsync( + seed: context => + { + context.AddRange( + new Context36837.EntityType + { + ComplexThing = new Context36837.ComplexThing { DateTime = new DateTime(2020, 1, 1) } + }); + return context.SaveChangesAsync(); + }); + + await using var context = contextFactory.CreateContext(); + + var count = await context.Set() + .CountAsync(b => b.ComplexThing == new Context36837.ComplexThing { DateTime = new DateTime(2020, 1, 1, 1, 1, 1, 999, 999) }); + Assert.Equal(0, count); + } + + private class Context36837(DbContextOptions options) : DbContext(options) + { + protected override void OnModelCreating(ModelBuilder modelBuilder) + => modelBuilder.Entity().ComplexProperty(b => b.ComplexThing); + + public class EntityType + { + public int Id { get; set; } + public ComplexThing ComplexThing { get; set; } = null!; + } + + public class ComplexThing + { + [Column(TypeName = "datetime")] // Non-default type mapping + public DateTime DateTime { get; set; } + } + } + + #endregion 36837 + protected override string StoreName => "AdHocComplexTypeQueryTest"; } diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/AdHocComplexTypeQuerySqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Query/AdHocComplexTypeQuerySqlServerTest.cs index 5705cedd074..c7f50ec4a40 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/AdHocComplexTypeQuerySqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/AdHocComplexTypeQuerySqlServerTest.cs @@ -32,6 +32,18 @@ FROM [EntityType] AS [e] """); } + public override async Task Complex_type_equality_with_non_default_type_mapping() + { + await base.Complex_type_equality_with_non_default_type_mapping(); + + AssertSql( + """ +SELECT COUNT(*) +FROM [EntityType] AS [e] +WHERE [e].[ComplexThing_DateTime] = '2020-01-01T01:01:01.999' +"""); + } + protected TestSqlLoggerFactory TestSqlLoggerFactory => (TestSqlLoggerFactory)ListLoggerFactory;