diff --git a/src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs b/src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs index b0127b440ec..b13b8a1102e 100644 --- a/src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs +++ b/src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs @@ -282,6 +282,19 @@ public void SetTables(IReadOnlyList tables) _tables.AddRange(tables); } + /// + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to + /// the same compatibility standards as public APIs. It may be changed or removed without notice in + /// any release. You should only use it directly in your code with extreme caution and knowing that + /// doing so can result in application failures when updating to a new Entity Framework Core release. + /// + [EntityFrameworkInternal] + public void ClearPredicate() + { + Check.DebugAssert(IsMutable, "Attempt to mutate an immutable SelectExpression"); + Predicate = null; + } + /// /// Applies a given set of tags. /// diff --git a/src/EFCore.SqlServer/Query/Internal/SqlServerQueryableMethodTranslatingExpressionVisitor.cs b/src/EFCore.SqlServer/Query/Internal/SqlServerQueryableMethodTranslatingExpressionVisitor.cs index 5ebacbbe9e6..92f3c99ece7 100644 --- a/src/EFCore.SqlServer/Query/Internal/SqlServerQueryableMethodTranslatingExpressionVisitor.cs +++ b/src/EFCore.SqlServer/Query/Internal/SqlServerQueryableMethodTranslatingExpressionVisitor.cs @@ -264,6 +264,7 @@ private Expression TranslateFullTextTableFunction( #pragma warning disable EF1001 // Internal EF Core API usage. select.SetTables([fullTextTableFunction]); + select.ClearPredicate(); #pragma warning restore EF1001 // Internal EF Core API usage. var keyProjectionMember = new ProjectionMember().Append(resultType.GetProperty(nameof(FullTextSearchResult.Key))!); diff --git a/test/EFCore.SqlServer.Tests/Query/FullTextTableQueryTest.cs b/test/EFCore.SqlServer.Tests/Query/FullTextTableQueryTest.cs new file mode 100644 index 00000000000..dd65b8cd697 --- /dev/null +++ b/test/EFCore.SqlServer.Tests/Query/FullTextTableQueryTest.cs @@ -0,0 +1,47 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace Microsoft.EntityFrameworkCore.Query; + +public class FullTextTableQueryTest +{ + private class Post + { + public int Id { get; set; } + public string Text { get; set; } = ""; + public bool IsDeleted { get; set; } + } + + private class BlogContext : DbContext + { + public DbSet Posts => Set(); + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + => optionsBuilder.UseSqlServer("Server=localhost;Database=Test;Trusted_Connection=True;TrustServerCertificate=True"); + + protected override void OnModelCreating(ModelBuilder modelBuilder) + => modelBuilder.Entity().HasQueryFilter(p => !p.IsDeleted); + } + + [Fact] + public void FreeTextTable_with_query_filter_does_not_generate_filter_with_undeclared_alias() + { + using var ctx = new BlogContext(); + var hits = ctx.Posts.FreeTextTable("search"); + var sql = hits.ToQueryString(); + + Assert.Contains("FREETEXTTABLE([Posts], *, @p)", sql); + Assert.DoesNotContain("WHERE", sql); + } + + [Fact] + public void ContainsTable_with_query_filter_does_not_generate_filter_with_undeclared_alias() + { + using var ctx = new BlogContext(); + var hits = ctx.Posts.ContainsTable("search"); + var sql = hits.ToQueryString(); + + Assert.Contains("CONTAINSTABLE([Posts], *, @p)", sql); + Assert.DoesNotContain("WHERE", sql); + } +}