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

Improve DbContext QueryFilter API #23067

Closed
BrandoCaserotti opened this issue Oct 21, 2020 · 1 comment
Closed

Improve DbContext QueryFilter API #23067

BrandoCaserotti opened this issue Oct 21, 2020 · 1 comment

Comments

@BrandoCaserotti
Copy link

At the time of writing this feature request we can declare a QueryFilter expression using ModelBuilder APIs ( HasQueryFilter ) in the OnModelCreating method of DbContext, using instance fields which are injected by constructor (eg. TenantId).

This approach is quite cumbersome if we want to set filter expressions for an entity programmatically (lets say that I want a queryFilter to be applied only if user has a lower permissions role or I want this QueryFilter to restrict data using an IN clause, where IN clause arguments are retrived from the database or an external API maybe for every incoming request in an asp.net core middleware).

Currently the DbContext must declare a QueryFilter definition internally and not from outside
(eg dbContextInstance.Filter<Post>(expr filter)) so this leads to complex code and bloated DbContext class by fields and many other methods to calculate a dynamic QueryFilter .

This open-source library provides a simpler way to do that and works really well, but I would like to see this feature in EF Core without relaying on an external library (maybe you could opt for a separate package lets say Microsoft.EntityFrameworkCore.QueryFilters)

The approach I suggested might bring the following improvements:

  • Programmatic queryfilter declarations
  • Cleaner DbContext code (no instance fields holding QueryFilter arguments)
  • No extra constructor parameters for QueryFilter configuration
@ajcvickers
Copy link
Member

@ZAP93 Thanks for posting; there are some interesting ideas here. I've added a line to #21459 to reference this issue, since this is where we are tracking general improvements to query filters.

@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

2 participants