Skip to content

False positive global query filter warning for owned entities #26900

@collinbarrett

Description

@collinbarrett

As mentioned here and similar to here, it seems like we may be getting a false positive global query filter warning (PossibleIncorrectRequiredNavigationWithQueryFilterInteractionWarning) for owned entities whose owners have a global filter query configured. Since, as far as I can tell, owned entities cannot be queried directly (apart from going through their owner) and they cannot have global query filters themselves, maybe we should not show this warning in this scenario.

Sample Code

using Microsoft.EntityFrameworkCore;

public class TestContext : DbContext
{
    public TestContext(DbContextOptions<TestContext> options) : base(options) { }

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

        modelBuilder.Entity<Blog>(
            e =>
            {
                e.HasQueryFilter(b => !b.IsDeleted);
                e.OwnsMany(b => b.Posts);
            });
    }
}

public class Blog
{
    public int BlogId { get; set; }
    public bool IsDeleted { get; set; }
    public ICollection<Post> Posts { 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; }
}

Include stack traces

N/A

Include verbose output

dotnet ef migrations add Test
Build started...
Build succeeded.
warn: 12/05/2021 08:26:55.418 CoreEventId.PossibleIncorrectRequiredNavigationWithQueryFilterInteractionWarning[10622] (Microsoft.EntityFrameworkCore.Model.Validation)
      Entity 'Blog' has a global query filter defined and is the required end of a relationship with the entity 'Post'. 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.
warn: 12/05/2021 08:26:55.424 CoreEventId.SensitiveDataLoggingEnabledWarning[10400] (Microsoft.EntityFrameworkCore.Infrastructure)
      Sensitive data logging is enabled. Log entries and exception messages may include sensitive application data; this mode should only be enabled during development.
info: 12/05/2021 08:26:55.607 CoreEventId.ContextInitialized[10403] (Microsoft.EntityFrameworkCore.Infrastructure)
      Entity Framework Core 6.0.0 initialized 'TestContext' using provider 'Npgsql.EntityFrameworkCore.PostgreSQL:6.0.0+025fc55189ae41e3f2b98bcbe4eb44c505653700' with options: SensitiveDataLoggingEnabled MaxPoolSize=1024 MigrationsAssembly=FilterLists.Directory.Infrastructure.Migrations using snake-case naming  (culture=)
warn: 12/05/2021 08:26:55.629 CoreEventId.PossibleIncorrectRequiredNavigationWithQueryFilterInteractionWarning[10622] (Microsoft.EntityFrameworkCore.Model.Validation)
      Entity 'Blog' has a global query filter defined and is the required end of a relationship with the entity 'Post'. 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.
Done. To undo this action, use 'ef migrations remove'

Include provider and version information

EF Core version: 6.0.0
Database provider: Npgsql.EntityFrameworkCore.PostgreSQL
Target framework: net6.0
Operating system: Windows 10
IDE: Visual Studio 2022

Metadata

Metadata

Assignees

Type

No fields configured for Bug.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions