Skip to content

Cannot override named query filters set via convention #37708

@pinkfloydx33

Description

@pinkfloydx33

Bug description

We're in the process of upgrading to EF 10. We have a Convention Set that automatically adds query filters to certain types. We are switching this over to named query filters and have noticed that once the convention sets the filter (with Configuration Source == Convention) that a user cannot override the filter with the same name explicitly

I think this is because of the difference in the code for adding global and named filters:

This code sets the configuration source explicitly:

public virtual EntityTypeBuilder HasQueryFilter(LambdaExpression? filter)
{
Builder.HasQueryFilter(filter, ConfigurationSource.Explicit);
return this;
}

While this code only calls the QueryFilter constructor and never sets a configuration source.

public virtual EntityTypeBuilder HasQueryFilter(string filterKey, LambdaExpression? filter)
{
Builder.HasQueryFilter(new QueryFilter(filterKey, filter));
return this;
}

Later on when performing CanSetQueryFilter, the configuration source on the new (explicit) filter is null and thus the old one wins.

Is this intended or is it a bug?

Your code

In a convention:

IConventionEntityTypeBuilder builder = ...;
builder.HasQueryFilter("SomeName", Expression.Lambda(...));

In a user provided configuration:

EntityTypeBuilder<SomeType> builder = ...;
builder.HasQueryFilter("SomeName", s => ...);  // <--- doesn't override

See: #37708 (comment) for a full reproduction

EF Core version

10.0.3

Target framework

.NET 10

Metadata

Metadata

Assignees

No one assigned

    Type

    No fields configured for Bug.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions