Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global query filter warning raised for TPH entities #26216

Closed
stevendarby opened this issue Sep 29, 2021 · 3 comments · Fixed by #28858
Closed

Global query filter warning raised for TPH entities #26216

stevendarby opened this issue Sep 29, 2021 · 3 comments · Fixed by #28858
Assignees
Labels
area-model-building area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@stevendarby
Copy link
Contributor

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

@stevendarby
Copy link
Contributor Author

stevendarby commented Oct 3, 2021

Think this might get fixed for 6.0 or is it being backlogged? I can have a go at putting together a PR if that helps.

edit: Have implemented a fix in fork, let me know if you want a PR raised in your repo, I understand you're close to wanting 6.0 pretty locked down.

stevendarby pushed a commit to stevendarby/efcore that referenced this issue Oct 3, 2021
@smitpatel
Copy link
Contributor

@stevendarby - We will likely to fix this in next release. While I agree this is a bug, but it is an incorrect warning, which user can either suppress (assuming all other warning of same kind are taken care of) or choose to ignore the warning. It can also be configured to just log rather than throw. So that way, this bug is not causing app to crash or not function properly. Hence it is not enough value to justify putting in 6.0.

cc: @dotnet/efteam - if anyone feels otherwise.

@stevendarby
Copy link
Contributor Author

Thanks for taking the time to answer, that seems entirely reasonable to me, at least for 6.0.0. I have some quibbles and frustrations with the approach to bug fixing between major versions, but that's not for discussion here. Can certainly live with this one for another year (or two for LTS!) :)

@ajcvickers ajcvickers modified the milestones: 6.0.0, Backlog Oct 5, 2021
@ajcvickers ajcvickers modified the milestones: Backlog, 7.0.0 Nov 3, 2021
@smitpatel smitpatel assigned smitpatel and unassigned maumar Aug 23, 2022
@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Aug 23, 2022
smitpatel added a commit that referenced this issue Aug 23, 2022
smitpatel added a commit that referenced this issue Aug 24, 2022
@smitpatel smitpatel modified the milestones: 7.0.0, 7.0.0-rc2 Aug 29, 2022
@ajcvickers ajcvickers modified the milestones: 7.0.0-rc2, 7.0.0 Nov 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-model-building area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants