Skip to content

Global query filter warning raised for TPH entities #26216

@stevendarby

Description

@stevendarby

Following code raises a warning:

Entity 'Blog' has a global query filter defined and is the required end of a relationship with the entity 'PicturePost'.
This may lead to unexpected results when the required entity is filtered out.
Either configure the navigation as optional, or define matching query filters for both entities in the navigation.
See https://go.microsoft.com/fwlink/?linkid=2131316 for more information.

I believe this is incorrect. You can only configure a filter on the base type. The base type, Post, has a filter specified. This flows down to PicturePost, so should be enough to satisfy the warning condition, but EF Core doesn't seem to detect this in its heuristics. If you change the collection navigation on Blog to collection of Posts instead, the warning disappears.

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;

{
    using var ctx = new MainContext();
    ctx.Database.EnsureDeleted();
    ctx.Database.EnsureCreated();
}

public class MainContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseSqlServer(@"Server=.;Database=FilterWarning;Integrated Security=True")
            .LogTo(Console.WriteLine, LogLevel.Warning);

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<Blog>(e => e.HasQueryFilter(b => b.IsDeleted == false));
        modelBuilder.Entity<Post>(e => e.HasQueryFilter(p => p.IsDeleted == false));
    }
}

public class Blog
{
    public int BlogId { get; set; }
    public bool IsDeleted { get; set; }
    public ICollection<PicturePost> PicturePosts { get; set; }
}

public class Post
{
    public int PostId { get; set; }
    public int BlogId { get; set; }
    public string Content { get; set; }
    public bool IsDeleted { get; set; }
    public Blog Blog { get; set; }
}

public class PicturePost : Post
{
    public string PictureUrl { get; set; }
}

EF Core version: 5.0, 6.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer

Metadata

Metadata

Assignees

Type

No fields configured for Bug.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions