Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/EFCore/Metadata/Builders/EntityTypeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ public virtual EntityTypeBuilder HasQueryFilter(LambdaExpression? filter)
/// <returns>The same builder instance so that multiple configuration calls can be chained.</returns>
public virtual EntityTypeBuilder HasQueryFilter(string filterKey, LambdaExpression? filter)
{
Builder.HasQueryFilter(new QueryFilter(filterKey, filter));
Builder.HasQueryFilter(new QueryFilter(filterKey, filter, ConfigurationSource.Explicit));

return this;
}
Expand Down
41 changes: 10 additions & 31 deletions src/EFCore/Metadata/Internal/QueryFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ namespace Microsoft.EntityFrameworkCore.Metadata.Internal;
/// </summary>
/// <param name="key">The key of the query filter.</param>
/// <param name="expression">The expression representing the filter.</param>
public class QueryFilter(string? key, LambdaExpression? expression) : IQueryFilter
/// <param name="configurationSource">The configuration source of the query filter.</param>
public class QueryFilter(string? key, LambdaExpression? expression, ConfigurationSource configurationSource) : IQueryFilter
{
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -32,7 +33,7 @@ public class QueryFilter(string? key, LambdaExpression? expression) : IQueryFilt
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual ConfigurationSource? ConfigurationSource { get; set; }
public virtual ConfigurationSource ConfigurationSource { get; set; } = configurationSource;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -41,27 +42,7 @@ public class QueryFilter(string? key, LambdaExpression? expression) : IQueryFilt
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public QueryFilter(LambdaExpression? expression, ConfigurationSource configurationSource)
: this(null, expression)
=> ConfigurationSource = configurationSource;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public QueryFilter(string key, LambdaExpression? expression, ConfigurationSource configurationSource)
: this(key, expression)
=> ConfigurationSource = configurationSource;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public QueryFilter(LambdaExpression? expression)
: this(null, expression)
: this(null, expression, configurationSource)
{
}

Expand All @@ -72,10 +53,9 @@ public QueryFilter(LambdaExpression? expression)
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public QueryFilter(string key, LambdaExpression? expression, bool fromDataAnnotation)
: this(key, expression)
=> ConfigurationSource = fromDataAnnotation
? Metadata.ConfigurationSource.DataAnnotation
: Metadata.ConfigurationSource.Convention;
: this(key, expression, fromDataAnnotation ? Metadata.ConfigurationSource.DataAnnotation : Metadata.ConfigurationSource.Convention)
{
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -84,8 +64,7 @@ public QueryFilter(string key, LambdaExpression? expression, bool fromDataAnnota
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public QueryFilter(LambdaExpression? expression, bool fromDataAnnotation)
: this(expression)
=> ConfigurationSource = fromDataAnnotation
? Metadata.ConfigurationSource.DataAnnotation
: Metadata.ConfigurationSource.Convention;
: this(null, expression, fromDataAnnotation ? Metadata.ConfigurationSource.DataAnnotation : Metadata.ConfigurationSource.Convention)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3392,6 +3392,31 @@ public void Can_replace_named_query_filter_only_with_lower_or_equal_source()
Assert.NotEqual(filterExpression2, entityBuilder.Metadata.FindDeclaredQueryFilter(filterKey).Expression);
}

[ConditionalFact]
public void Can_override_named_query_filter_from_convention_with_explicit_configuration()
{
// This test verifies that named query filters set by conventions can be overridden by explicit configuration
var modelBuilder = CreateModelBuilder();
var entityBuilder = modelBuilder.Entity(typeof(Order), ConfigurationSource.Explicit);

LambdaExpression conventionFilter = (Order o) => o.Id == 1;
LambdaExpression explicitFilter = (Order o) => o.Id == 2;
const string filterKey = "testFilter";

// Convention sets a named query filter
entityBuilder.HasQueryFilter(new QueryFilter(filterKey, conventionFilter, ConfigurationSource.Convention));
Assert.Same(conventionFilter, entityBuilder.Metadata.FindDeclaredQueryFilter(filterKey).Expression);
Assert.Equal(ConfigurationSource.Convention, entityBuilder.Metadata.GetQueryFilterConfigurationSource(filterKey));

// Public API should be able to override it
var publicBuilder = new EntityTypeBuilder(entityBuilder.Metadata);
publicBuilder.HasQueryFilter(filterKey, explicitFilter);

// Verify the filter was replaced with the explicit one
Assert.Same(explicitFilter, entityBuilder.Metadata.FindDeclaredQueryFilter(filterKey).Expression);
Assert.Equal(ConfigurationSource.Explicit, entityBuilder.Metadata.GetQueryFilterConfigurationSource(filterKey));
}

private static TestLogger<DbLoggerCategory.Model, TestLoggingDefinitions> CreateTestLogger()
=> new() { EnabledFor = LogLevel.Warning };

Expand Down
Loading