dotnet data_error ai_generated true

System.InvalidOperationException: An error was generated for the query filter 'FilterName'. The filter expression must be a LambdaExpression that can be applied to the entity type 'EntityType'.

ID: dotnet/ef-core-query-filter-parameter-mismatch

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
1Evidence
2023-06-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
6.0 active
7.0 active
8.0 active
9.0 active

Root Cause

Entity Framework Core query filter uses a parameterized expression that references a property or method not available on the entity's model, often due to a missing navigation property or incorrect lambda syntax.

generic

中文

Entity Framework Core 查询过滤器使用了参数化表达式,引用了实体模型上不可用的属性或方法,通常是由于缺少导航属性或 Lambda 语法错误。

Official Documentation

https://learn.microsoft.com/en-us/ef/core/querying/filters

Workarounds

  1. 85% success Rewrite the query filter in OnModelCreating to use a simple lambda without complex expressions. For example, change 'modelBuilder.Entity<Blog>().HasQueryFilter(b => b.TenantId == _tenantProvider.GetTenantId())' to 'modelBuilder.Entity<Blog>().HasQueryFilter(b => b.TenantId == 1)' and pass the tenant ID via a constructor parameter.
    Rewrite the query filter in OnModelCreating to use a simple lambda without complex expressions. For example, change 'modelBuilder.Entity<Blog>().HasQueryFilter(b => b.TenantId == _tenantProvider.GetTenantId())' to 'modelBuilder.Entity<Blog>().HasQueryFilter(b => b.TenantId == 1)' and pass the tenant ID via a constructor parameter.
  2. 80% success Ensure that any method used in the filter is a static method or property accessible at the model level. Replace instance methods with static equivalents: 'b => b.IsDeleted == false' instead of 'b => _service.IsDeleted(b)'.
    Ensure that any method used in the filter is a static method or property accessible at the model level. Replace instance methods with static equivalents: 'b => b.IsDeleted == false' instead of 'b => _service.IsDeleted(b)'.

中文步骤

  1. Rewrite the query filter in OnModelCreating to use a simple lambda without complex expressions. For example, change 'modelBuilder.Entity<Blog>().HasQueryFilter(b => b.TenantId == _tenantProvider.GetTenantId())' to 'modelBuilder.Entity<Blog>().HasQueryFilter(b => b.TenantId == 1)' and pass the tenant ID via a constructor parameter.
  2. Ensure that any method used in the filter is a static method or property accessible at the model level. Replace instance methods with static equivalents: 'b => b.IsDeleted == false' instead of 'b => _service.IsDeleted(b)'.

Dead Ends

Common approaches that don't work:

  1. 60% fail

    Removing the filter entirely may break business logic that requires soft-delete or multi-tenant isolation.

  2. 80% fail

    Adding .Include() to eager-load navigation properties does not fix the filter expression itself; the filter must be corrected at the model level.

  3. 70% fail

    Changing the entity's base class or interface does not address the lambda parameter mismatch; the filter must be rewritten.