Skip to content

Commit a063616

Browse files
committed
Clear predicate when translating full-text table-valued functions (#39686)
1 parent bfd4aef commit a063616

3 files changed

Lines changed: 61 additions & 0 deletions

File tree

‎src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,19 @@ public void SetTables(IReadOnlyList<TableExpressionBase> tables)
282282
_tables.AddRange(tables);
283283
}
284284

285+
/// <summary>
286+
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
287+
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
288+
/// any release. You should only use it directly in your code with extreme caution and knowing that
289+
/// doing so can result in application failures when updating to a new Entity Framework Core release.
290+
/// </summary>
291+
[EntityFrameworkInternal]
292+
public void ClearPredicate()
293+
{
294+
Check.DebugAssert(IsMutable, "Attempt to mutate an immutable SelectExpression");
295+
Predicate = null;
296+
}
297+
285298
/// <summary>
286299
/// Applies a given set of tags.
287300
/// </summary>

‎src/EFCore.SqlServer/Query/Internal/SqlServerQueryableMethodTranslatingExpressionVisitor.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ private Expression TranslateFullTextTableFunction(
264264

265265
#pragma warning disable EF1001 // Internal EF Core API usage.
266266
select.SetTables([fullTextTableFunction]);
267+
select.ClearPredicate();
267268
#pragma warning restore EF1001 // Internal EF Core API usage.
268269

269270
var keyProjectionMember = new ProjectionMember().Append(resultType.GetProperty(nameof(FullTextSearchResult<int>.Key))!);
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.EntityFrameworkCore.Query;
5+
6+
public class FullTextTableQueryTest
7+
{
8+
private class Post
9+
{
10+
public int Id { get; set; }
11+
public string Text { get; set; } = "";
12+
public bool IsDeleted { get; set; }
13+
}
14+
15+
private class BlogContext : DbContext
16+
{
17+
public DbSet<Post> Posts => Set<Post>();
18+
19+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
20+
=> optionsBuilder.UseSqlServer("Server=localhost;Database=Test;Trusted_Connection=True;TrustServerCertificate=True");
21+
22+
protected override void OnModelCreating(ModelBuilder modelBuilder)
23+
=> modelBuilder.Entity<Post>().HasQueryFilter(p => !p.IsDeleted);
24+
}
25+
26+
[Fact]
27+
public void FreeTextTable_with_query_filter_does_not_generate_filter_with_undeclared_alias()
28+
{
29+
using var ctx = new BlogContext();
30+
var hits = ctx.Posts.FreeTextTable<Post, int>("search");
31+
var sql = hits.ToQueryString();
32+
33+
Assert.Contains("FREETEXTTABLE([Posts], *, @p)", sql);
34+
Assert.DoesNotContain("WHERE", sql);
35+
}
36+
37+
[Fact]
38+
public void ContainsTable_with_query_filter_does_not_generate_filter_with_undeclared_alias()
39+
{
40+
using var ctx = new BlogContext();
41+
var hits = ctx.Posts.ContainsTable<Post, int>("search");
42+
var sql = hits.ToQueryString();
43+
44+
Assert.Contains("CONTAINSTABLE([Posts], *, @p)", sql);
45+
Assert.DoesNotContain("WHERE", sql);
46+
}
47+
}

0 commit comments

Comments
 (0)