Skip to content

Commit c156ea8

Browse files
committed
Fixup
1 parent 9c336c0 commit c156ea8

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

entity-framework/core/what-is-new/ef-core-10.0/breaking-changes.md

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,14 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
141141

142142
#### Old behavior
143143

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 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:
145145

146146
```c#
147147
int[] ids = [1, 2, 3];
148148
var blogs = await context.Blogs.Where(b => ids.Contains(b.Id)).ToListAsync();
149149
```
150150

151-
This generated SQL like:
151+
On SQL Server, this generated the following SQL:
152152

153153
```sql
154154
@__ids_0='[1,2,3]'
@@ -171,10 +171,6 @@ FROM [Blogs] AS [b]
171171
WHERE [b].[Id] IN (@ids1, @ids2, @ids3)
172172
```
173173

174-
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-
178174
#### Why
179175

180176
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
183179

184180
#### Mitigations
185181

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:
187183

188184
```c#
189185
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
@@ -193,15 +189,28 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
193189
```
194190

195191
Available modes are:
192+
196193
- `ParameterTranslationMode.MultipleParameters` - The new default (multiple scalar parameters)
197-
- `ParameterTranslationMode.Constant` - Inlines values as constants (pre-EF8 behavior)
198-
- `ParameterTranslationMode.SingleJsonParameter` - Uses JSON array parameter (EF8-9 default)
194+
- `ParameterTranslationMode.Constant` - Inlines values as constants (pre-EF8 default behavior)
195+
- `ParameterTranslationMode.Parameter` - Uses JSON array parameter (EF8-9 default)
199196

200197
You can also control the translation on a per-query basis:
201198

202199
```c#
203200
// Use constants instead of parameters for this specific query
204-
var blogs = await context.Blogs.Where(b => EF.Constant(ids).Contains(b.Id)).ToListAsync();
201+
var blogs = await context.Blogs
202+
.Where(b => EF.Constant(ids).Contains(b.Id))
203+
.ToListAsync();
204+
205+
// Use a single parameter (e.g. JSON parameter with OPENJSON) instead of parameters for this specific query
206+
var blogs = await context.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+
var blogs = await context.Blogs
212+
.Where(b => EF.Parameter(ids).Contains(b.Id))
213+
.ToListAsync();
205214
```
206215

207216
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

Comments
 (0)