skip to content

After upgrading to EF Core 10, a query using ids.Contains(w.ProjectId) sends different SQL; what changed, and how do EF.Constant, EF.Parameter and UseParameterizedCollectionMode control it?

level: seniorimportance: should knowfreq 36%

answer

  1. parameterized collection, three strategies
  2. EF 8-9: one JSON array parameter
  3. EF 10 default: IN (@ids1, @ids2, ...)
  4. Npgsql keeps one array parameter: = ANY (@ids)
  5. ParameterTranslationMode: MultipleParameters, Constant, Parameter

basics

~20 s

EF Core 10 translates a captured collection into several scalar parameters, IN (@ids1, @ids2, ...), instead of EF 8-9's single JSON array parameter. UseParameterizedCollectionMode sets the default per context; EF.Constant, EF.Parameter and EF.MultipleParameters override it for one query.

solid answer

~40 s

A local list used in `Contains` is a *parameterized collection*. EF Core 8 and 9 sent it as one JSON array parameter unpacked in SQL (with `OPENJSON` on SQL Server): one plan for any list, but the planner could not see how many values there were. EF Core 10's relational default is `ParameterTranslationMode.MultipleParameters`: `IN (@ids1, @ids2, @ids3)`, padded to a bucket size so lists of similar length share SQL, giving the planner the cardinality. You can change the default with `UseParameterizedCollectionMode(ParameterTranslationMode.Constant | Parameter | MultipleParameters)` on the provider options, or per query by wrapping the list in `EF.Constant(ids)`, `EF.Parameter(ids)` or `EF.MultipleParameters(ids)`. Providers may choose their own default: Npgsql 10 keeps `Parameter`, sending one PostgreSQL array as `= ANY (@ids)`. EF Core 10 also redacts inlined constants as `?` in logged SQL.

code

csharp · 9 lines
csharp
// Small, stable lists of statuses: plan per list is worth it
var open = await db.WorkItems
    .Where(w => EF.Constant(statuses).Contains(w.Status))
    .ToListAsync();

// Large, varied list: one array parameter, one plan
var items = await db.WorkItems
    .Where(w => EF.Parameter(projectIds).Contains(w.ProjectId))
    .ToListAsync();

go deeper

for a junior

Know that a local list used with Contains is sent to SQL as parameters, and that EF Core 10 changed how.

for a middle

Explain the three ParameterTranslationMode strategies, what each does to the SQL, and how to override one query with EF.Constant or EF.Parameter.

for a senior

Diagnose plan regressions after an EF Core 10 upgrade, account for provider defaults such as Npgsql's array parameter, and choose overrides per query with measurements.

for a principal

Plan the 8-to-10 upgrade's risk: identify list-heavy queries, agree on a default mode per database, and monitor plan cache behaviour.

## What a parameterized collection is On the project dashboard a user picks several projects, and the API filters work items by them: ```csharp int[] ids = request.ProjectIds; var items = await db.WorkItems.Where(w => ids.Contains(w.ProjectId)).ToListAsync(); ``` `ids` is captured from C#, so it changes on every call: a **parameterized collection**. Relational databases have no portable way to bind a list as one parameter, so EF Core has used three strategies over the years. ## The three translations | `ParameterTranslationMode` | SQL shape (SQL Server examples from the EF docs) | Default in | |---|---|---| | `Constant` | `IN (1, 2, 3)` | EF Core 7 and earlier | | `Parameter` | one array-like parameter, e.g. `IN (SELECT [i].[value] FROM OPENJSON(@ids) ...)` | EF Core 8 and 9 | | `MultipleParameters` | `IN (@ids1, @ids2, @ids3)` | EF Core 10 (relational default) | - **Constants** give the planner exact values but produce different SQL for every list, bloating the database plan cache and EF's own query cache. - **A single array parameter** keeps one SQL text and one plan for any list, but the planner does not know whether the list holds 2 values or 20,000, and may pick a plan that suits neither. - **Multiple scalar parameters** keep SQL stable for lists of similar size and tell the planner the cardinality. To avoid a new SQL text for every length, EF **pads** the list: eight values are sent as ten parameters, the last two repeating the eighth value so results do not change. EF Core 10 also **simplified parameter names**: you see `@ids1` and `@projectId` instead of names like `@__ids_0`. ## Provider defaults differ The relational base default is `MultipleParameters`, but a provider can override it. **Npgsql's EF Core provider 10 keeps `Parameter` as its default**, because PostgreSQL has native arrays: the query above becomes `WHERE w."ProjectId" = ANY (@ids)` with a single `integer[]` parameter. So the EF Core 10 breaking change is most visible on SQL Server; on PostgreSQL the SQL stays an array comparison unless you choose otherwise. ## Controlling it Globally, on the provider options builder: ```csharp builder.Services.AddDbContext<TrackerDbContext>(o => o.UseNpgsql(connectionString, npgsql => npgsql.UseParameterizedCollectionMode(ParameterTranslationMode.MultipleParameters))); ``` Per query, by wrapping the collection; the wrapper overrides the global mode for that query only: 1. `EF.Constant(ids)` inlines the values. Useful when the set of lists is small and stable (a handful of status codes) and a per-list plan pays off. 2. `EF.Parameter(ids)` sends one array-like parameter. 3. `EF.MultipleParameters(ids)` sends one parameter per value, useful when the global default was changed. The same two helpers work on scalars: `EF.Constant(x)` inlines a captured value, and `EF.Parameter(5)` parameterizes a literal that would otherwise be inlined, which helps when a query is built dynamically with many different literals. The older options `TranslateParameterizedCollectionsToConstants()` and `TranslateParameterizedCollectionsToParameters()` are marked `[Obsolete]` in favour of `UseParameterizedCollectionMode`. ## Upgrade judgement - Most queries get the same or better plans after the change. - Queries with **very large lists** turn into very long parameter lists (the SQL Server provider switches to another translation as it nears the server's parameter limit), and some of them may have relied on the single-parameter plan. Measure those, and revert per query with `EF.Parameter`. - **Inlined constants are redacted in EF Core 10 logs**: `EF.Constant(roles)` logs `IN (?, ?)` unless sensitive data logging is on, so switching to constants no longer leaks values into application logs.

  • Why does EF Core 10 send ten parameters when the list has eight values?
    Every distinct list length would otherwise produce a distinct SQL text and a new plan. EF pads the parameter list to a bucket size, filling the extra slots with the last value, so lists of similar lengths share one SQL text while the results stay the same.
  • When is EF.Parameter useful for a scalar rather than a collection?
    When the query contains a literal that EF would inline as a constant, for example in dynamically built queries. `EF.Parameter(value)` forces it into a parameter so different values reuse the same SQL text and cached query instead of creating one per value.

  • EF Core 10 still sends the list as one OPENJSON parameter on SQL Server.
  • Inlining collections as constants never affects the plan cache.
  • Every provider uses the same default ParameterTranslationMode.
  • EF.Constant values appear in EF Core 10 logs by default.
  • Contains on a local list cannot be translated and runs on the client.

Assumes EF Core 10 with Npgsql 10.0.3; EF Core 8 and 9 defaulted to a single JSON array parameter, and EF Core 10's relational default is multiple scalar parameters.