Skip to content

Support for navigation references in model-level query filters #9539

Closed as duplicate

Description

@pauldalyii

I would like to be able to use navigation references in model-level query filters. This limitation is called out in the ef core / what's new documentation.

I can probably best describe my use case by extending the tenant sample shown in that documentation.

Unlike the referenced sample, I do not have reference to the TenantId foreign key at the time of context creation, therefore I can not reference it in the HasQueryFilter method.

At the time of context creation, I do have reference to another unique property of a tenant: TenantName . (e.g.: "tenant.onmicrosoft.com")

Here is an example of the model:

public class Tenant
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public List<Blog> Blogs { get; set; }
}
public class Blog
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public Tenant Tenant { get; set; }
    public Guid TenantId { get; set; }
    ...
}

In OnModelCreating, I can filter the Tenant entity on a private property _tenantName:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Tenant>().Property(t => t.Name).IsRequired();
    modelBuilder.Entity<Tenant>().HasAlternateKey(t => t.Name);
    modelBuilder.Entity<Tenant>().HasQueryFilter(t => t.Name == _tenantName);
    ...
}

I need to filter a number of other entities by Tenant, and those entities only directly reference the Tenant via a foreign key: TenantId. I'd like to be able use the navigation reference "b.Tenant.Name" in the following manner:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    ...
    modelBuilder.Entity<Blog>().HasQueryFilter(b => b.Tenant.Name == _tenantName);
    ...
}

I think this is important because the value of a filter applied in OnModelCreating is to not have to remember to filter anywhere else. As it is implemented currently, I still need to pay attention anytime I interact with something other than the Tenant model.

Activity

  1. smitpatel commented on Aug 23, 2017

    @smitpatel
    Contributor

    duplicate of #8881

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions