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

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

Closed
pauldalyii opened this issue Aug 23, 2017 · 1 comment
Closed

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

pauldalyii opened this issue Aug 23, 2017 · 1 comment

Comments

@pauldalyii
Copy link

pauldalyii commented Aug 23, 2017

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.

@smitpatel
Copy link
Contributor

duplicate of #8881

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

3 participants