Skip to content

Commit 7c17ae9

Browse files
authored
Document stripping of no-op SQL CASTs (#5348)
Document dotnet/efcore#36247
1 parent 0e81311 commit 7c17ae9

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

  • entity-framework/core/what-is-new/ef-core-11.0

entity-framework/core/what-is-new/ef-core-11.0/whatsnew.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,30 @@ Both optimizations can have a significant positive impact on query performance,
164164

165165
More details on the benchmark are available [here](https://github.com/dotnet/efcore/issues/29182#issuecomment-4231140289), and as always, actual performance in your application will vary based on your schema, data and a variety of other factors.
166166

167+
<a name="linq-strip-noop-casts"></a>
168+
169+
### Stripping of no-op CASTs
170+
171+
EF Core sometimes generated SQL `CAST` expressions that converted a column to the type it already stores — for example, `CAST([u].[Name] AS nvarchar(max))` on a column that is already `nvarchar(max)`. This commonly occurred with [value-converted](xref:core/modeling/value-conversions) properties whose CLR type has an implicit conversion to the provider type, but could also happen in other scenarios. Such redundant casts produce unnecessary work and can prevent the database from using indexes on the column.
172+
173+
EF Core 11 now detects and strips these no-op `CAST` expressions during SQL post-processing. For example, the following query:
174+
175+
```sql
176+
SELECT [u].[Id], [u].[Name]
177+
FROM [Users] AS [u]
178+
WHERE CAST([u].[Name] AS nvarchar(max)) LIKE N'Name%'
179+
```
180+
181+
Now generates cleaner SQL:
182+
183+
```sql
184+
SELECT [u].[Id], [u].[Name]
185+
FROM [Users] AS [u]
186+
WHERE [u].[Name] LIKE N'Name%'
187+
```
188+
189+
This allows the database to use any index defined on the column, which can significantly improve query performance.
190+
167191
<a name="linq-maxby-minby"></a>
168192

169193
### MaxBy and MinBy

0 commit comments

Comments
 (0)