You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In EF Core 9 and earlier, parameterized collections in LINQ queries (such as those used with `.Contains()`) were translated to SQL using a JSON array parameter with `OPENJSON` on SQL Server:
144
+
In EF Core 9 and earlier, parameterized collections in LINQ queries (such as those used with `.Contains()`) were translated to SQL using a JSON array parameter by default. Consider the following query:
When the number of values in the collection approaches limits (e.g., SQL Server's 2,100 parameter limit), this can cause runtime errors such as `System.DivideByZeroException` or exceed database parameter limits.
175
-
176
-
EF also "pads" the parameter list to reduce the number of different SQL statements generated. For example, a collection with 8 values generates SQL with 10 parameters, with the extra parameters containing duplicate values.
177
-
178
174
#### Why
179
175
180
176
The new default translation provides the query planner with cardinality information about the collection, which can lead to better query plans in many scenarios. The multiple parameter approach balances between plan cache efficiency (by parameterizing) and query optimization (by providing cardinality).
@@ -183,7 +179,7 @@ However, different workloads may benefit from different translation strategies d
183
179
184
180
#### Mitigations
185
181
186
-
If you encounter issues with the new default behavior (such as parameter limit errors or performance regressions), you can configure the translation mode globally:
182
+
If you encounter issues with the new default behavior (such as performance regressions), you can configure the translation mode globally:
// Use a single parameter (e.g. JSON parameter with OPENJSON) instead of parameters for this specific query
206
+
varblogs=awaitcontext.Blogs
207
+
.Where(b=>EF.Parameter(ids).Contains(b.Id))
208
+
.ToListAsync();
209
+
210
+
// Use a multiple scalar parameters for this specific query. This is the default in EF 10, but is useful if the default was changed globally:
211
+
varblogs=awaitcontext.Blogs
212
+
.Where(b=>EF.Parameter(ids).Contains(b.Id))
213
+
.ToListAsync();
205
214
```
206
215
207
216
For more information about parameterized collection translation, [see the documentation](xref:core/what-is-new/ef-core-10.0/whatsnew#parameterized-collection-translation).
0 commit comments