You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
QueryFilter: Override similar FromSql, to be applied globally #15239
Feature request to add a HasQueryFilter(FormattedString sql) override that behaves similarly to FromSql on all LINQ queries involving the entity type, including through Include or direct navigation property references.
Use Case
A multi-tenancy environment where a tenant can get access to more than one record. Even though HasQueryFilter works across navigation properties, it results in a SQL WHERE. If a second table has to be included, using an INNER JOIN instead of a WHERE EXISTS gives faster SQL execution time.
protectedoverridevoidOnModelCreating(ModelBuildermodelBuilder){// With current LINQ query predicate functionality// modelBuilder.Entity<Blog>().HasQueryFilter(b => b.BlogTenants.Any(bt => bt.TenantId == _tenantId));// With requested SQL string functionalitymodelBuilder.Entity<Blog>().HasQueryFilter($"SELECT b.* FROM Blog b INNER JOIN BlogTenants bt ON bt.TenantId = {_tenantId} AND bt.BlogId = b.BlogId");}
Alternatively, if HasQueryFilter can take a Func<IQueryable<T>, IQueryable<T>> to allow developers to chain Linq statements onto the source, then we can create Linq joins or use FromSql on it.
I appreciate the suggestion. Unless I'm missing something, ToQuery is only on DbQuery. I do now want to convert my Entity to a Query for two reasons:
I cannot have a DbQuery as the "One" side of a one-to-many relationship in navigation properties. e.g. I wouldn't be able to have a "UserFavoriteBlogs" with a "BlogId" and "Blog" navigation property that applies this query filter.
I need to be able to create and update Blog records, and I cannot do that with DbQuery. I would need to create a separate DbSet for the same table for writes.
One alternative I considered - similar to your suggestion - is to have saved queries on the database, e.g. "FilteredBlogs", that joins the tables. I would define these queries in the DbContext pretending they're tables through DbSet so I can create navigation properties between these "Filtered" queries. Those would be used exclusively for reads, and separate DbSets to the actual tables would be used exclusively for writes.
I'd rather not do that, mostly because of the additional hassle of maintaining saved queries (and potential changes) in a code-first model.
The main appeal of HasQueryFilter is that it applies across Include and navigation properties, while other methods like LINQ .Where or FromSql cannot. Unless I'm missing something, it's for that distinction that HasQueryFilter exists at all: whenever that table is accessed through DbContext, that rule is applied. (With exception to directly executing a provided sql query, such as with FromSql or ExecuteSqlCommand.)
Notes from triage: the core functionality requested here is the same as that for defining query (#13358), so closing this as a dupe with a reference from there to this scenario.
Activity
zamzowd commentedon Apr 4, 2019
Alternatively, if
HasQueryFiltercan take aFunc<IQueryable<T>, IQueryable<T>>to allow developers to chain Linq statements onto the source, then we can create Linq joins or useFromSqlon it.smitpatel commentedon Apr 4, 2019
Have you tried using ToQuery method?
zamzowd commentedon Apr 4, 2019
I appreciate the suggestion. Unless I'm missing something, ToQuery is only on DbQuery. I do now want to convert my Entity to a Query for two reasons:
One alternative I considered - similar to your suggestion - is to have saved queries on the database, e.g. "FilteredBlogs", that joins the tables. I would define these queries in the DbContext pretending they're tables through DbSet so I can create navigation properties between these "Filtered" queries. Those would be used exclusively for reads, and separate DbSets to the actual tables would be used exclusively for writes.
I'd rather not do that, mostly because of the additional hassle of maintaining saved queries (and potential changes) in a code-first model.
smitpatel commentedon Apr 4, 2019
How about creating additional IQueryable on DbContext?
DbContext could have
IQueryable<Blog> FilteredBlogs => Set<Blog>.FromSql(....);zamzowd commentedon Apr 4, 2019
The main appeal of
HasQueryFilteris that it applies acrossIncludeand navigation properties, while other methods like LINQ.WhereorFromSqlcannot. Unless I'm missing something, it's for that distinction thatHasQueryFilterexists at all: whenever that table is accessed through DbContext, that rule is applied. (With exception to directly executing a provided sql query, such as withFromSqlorExecuteSqlCommand.)ajcvickers commentedon Apr 8, 2019
Notes from triage: the core functionality requested here is the same as that for defining query (#13358), so closing this as a dupe with a reference from there to this scenario.