Skip to content

QueryFilter: Override similar FromSql, to be applied globally #15239

Closed as duplicate

Description

@zamzowd

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.

Entities:

public class Blog
{
  public int BlogId { get; set; }
  public string Name { get; set; }
  public string Url { get; set; }

  public ICollection<BlogTenant> BlogTenants { get; set; }
}

public class BlogTenant
{
  public int BlogId { get; set; }
  public int TenantId { get; set; }
}

OnModelCreating:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
  // With current LINQ query predicate functionality
  // modelBuilder.Entity<Blog>().HasQueryFilter(b => b.BlogTenants.Any(bt => bt.TenantId == _tenantId));

  // With requested SQL string functionality
  modelBuilder.Entity<Blog>().HasQueryFilter(
    $"SELECT b.* FROM Blog b INNER JOIN BlogTenants bt ON bt.TenantId = {_tenantId} AND bt.BlogId = b.BlogId"
  );
}

Activity

  1. zamzowd commented on Apr 4, 2019

    @zamzowd
    Author

    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.

  2. smitpatel commented on Apr 4, 2019

    @smitpatel
    Contributor

    Have you tried using ToQuery method?

  3. zamzowd commented on Apr 4, 2019

    @zamzowd
    Author

    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:

    1. 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.
    2. 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.

  4. smitpatel commented on Apr 4, 2019

    @smitpatel
    Contributor

    How about creating additional IQueryable on DbContext?
    DbContext could have
    IQueryable<Blog> FilteredBlogs => Set<Blog>.FromSql(....);

  5. zamzowd commented on Apr 4, 2019

    @zamzowd
    Author

    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.)

  6. ajcvickers commented on Apr 8, 2019

    @ajcvickers
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions