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 Filters filtering related entities #14151

Closed
hisuwh opened this issue Dec 12, 2018 · 5 comments
Closed

Global Query Filters filtering related entities #14151

hisuwh opened this issue Dec 12, 2018 · 5 comments

Comments

@hisuwh
Copy link

hisuwh commented Dec 12, 2018

We are querying an entity and including an entity which has a global query filter. This entity is filtered out as consequence of this global query filter. This is either a bug or an undocumented feature but either way we need a workaround to this.

Steps to reproduce

Given this model:

class Author 
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool Archived { get; set; }
}

class BlogPost
{
    public int Id { get; set; }
    public string Title { get; set; }
    public int AuthorId { get; set; }
    public Author Author { get; set; }
}

class BlogContext
{
    public DbSet<Author> Authors { get; set; }
    public DbSet<BlogPost> BlogPosts { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder) 
    {
        modelBuilder.Entity<Author>()
             .HasQueryFilter(a => !a.Archived);
    }
}

I might have an API endpoint to return archived Authors and their posts.
Now say I call my API endpoint to return my blog post which is implemented like this:

public BlogPost Get(int id) {
    return this.context.BlogPosts
        .Include(b => b.Author)
        .SingleOrDefault(b => b.Id == id);
}

This returns null because it appears to be filtering out the BlogPost because its author is archived. Now I realise I could add .IgnoreGlobalFilters() here but I do not want to do this because this is my generic endpoint to return BlogPosts, and I might later add another Global Filter (i.e. !Deleted).

Possible Solutions

  1. This needs documenting as it is not clear that this will happen currently
  2. The behaviour is not the same if the relationship is optional (so int? AuthorId in the above example) - not sure if this is intended but this also needs documenting if so
  3. We need a workaround
  • this approach is not correct in all scenarios, I can see why you would want this for Deleted for example but it doesn't make sense in my case
  • ideally this would be overridable on setup:
modelBuilder.Entity<Author>()
    .HasQueryFilter(a => !a.Archived, filterRelationships: false) // or a better name ;)
  • worst case some way to ignore this when filtering:
return this.context.BlogPosts
        .Include(b => b.Author, ignoreGlobalFilters: true)
        .SingleOrDefault(b => b.Id == id);

Further technical details

EF Core version: 2.2.0
Database Provider: Microsoft.EntityFrameworkCore.SqlServer)
Operating system: Windows 10
IDE: isual Studio 2017 15.7.6

@smitpatel
Copy link
Member

Duplicate of #11691

@smitpatel smitpatel marked this as a duplicate of #11691 Dec 12, 2018
@hisuwh
Copy link
Author

hisuwh commented Dec 13, 2018

Sorry I did search for similar issues but did not come across that other one

@jonesty
Copy link

jonesty commented Jun 10, 2020

@hisuwh I know this is an old issue but what approach did you end up taking with this? I'm currently dealing with the same scenario. Cheers.

@hisuwh
Copy link
Author

hisuwh commented Jun 10, 2020

@jonesty I think we ended up not using global filters

@jonesty
Copy link

jonesty commented Jun 10, 2020

@hisuwh Thanks mate.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants