diff --git a/src/EFCore/Query/Internal/ParameterExtractingExpressionVisitor.cs b/src/EFCore/Query/Internal/ParameterExtractingExpressionVisitor.cs index e4b2b4b184d..3d6ce34e6bd 100644 --- a/src/EFCore/Query/Internal/ParameterExtractingExpressionVisitor.cs +++ b/src/EFCore/Query/Internal/ParameterExtractingExpressionVisitor.cs @@ -349,11 +349,10 @@ public ContextParameterReplacingExpressionVisitor(Type contextType) public ParameterExpression ContextParameterExpression { get; } public override Expression Visit(Expression expression) - { - return expression?.Type.GetTypeInfo().IsAssignableFrom(_contextType) == true + => expression?.Type != typeof(object) + && expression?.Type.GetTypeInfo().IsAssignableFrom(_contextType) == true ? ContextParameterExpression : base.Visit(expression); - } } private static Expression RemoveConvert(Expression expression) diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/QueryBugsTest.cs b/test/EFCore.SqlServer.FunctionalTests/Query/QueryBugsTest.cs index 6bc15fc9cec..343093849ee 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/QueryBugsTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/QueryBugsTest.cs @@ -7361,6 +7361,57 @@ public BugContext18087(DbContextOptions options) #endregion + #region Issue18759 + + [ConditionalFact] + public void Query_filter_with_null_constant() + { + using var _ = CreateDatabase18759(); + using var context = new BugContext18759(_options); + + var people = context.People.ToList(); + + AssertSql( + @"SELECT [p].[Id], [p].[UserDeleteId] +FROM [People] AS [p] +LEFT JOIN [User18759] AS [u] ON [p].[UserDeleteId] = [u].[Id] +WHERE [u].[Id] IS NOT NULL"); + } + + private SqlServerTestStore CreateDatabase18759() + => CreateTestStore( + () => new BugContext18759(_options), + context => + { + ClearLog(); + }); + + public class Person18759 + { + public int Id { get; set; } + public User18759 UserDelete { get; set; } + } + + public class User18759 + { + public int Id { get; set; } + } + + private class BugContext18759 : DbContext + { + public DbSet People { get; set; } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + => modelBuilder.Entity().HasQueryFilter(p => p.UserDelete != null); + + public BugContext18759(DbContextOptions options) + : base(options) + { + } + } + + #endregion Issue18759 + private DbContextOptions _options; private SqlServerTestStore CreateTestStore(