# System.InvalidOperationException: 为查询过滤器 'FilterName' 生成了错误。筛选器表达式必须是可应用于实体类型 'EntityType' 的 LambdaExpression。

- **ID:** `dotnet/ef-core-query-filter-parameter-mismatch`
- **领域:** dotnet
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 6.0 | active | — | — |
| 7.0 | active | — | — |
| 8.0 | active | — | — |
| 9.0 | active | — | — |

## 解决方案

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)'.
   ```

## 无效尝试

- **** — Removing the filter entirely may break business logic that requires soft-delete or multi-tenant isolation. (60% 失败率)
- **** — Adding .Include() to eager-load navigation properties does not fix the filter expression itself; the filter must be corrected at the model level. (80% 失败率)
- **** — Changing the entity's base class or interface does not address the lambda parameter mismatch; the filter must be rewritten. (70% 失败率)
