From 5fd60fb8a0f72e830c6124a18f1207fca12aaebd Mon Sep 17 00:00:00 2001 From: Shay Rojansky Date: Tue, 28 Apr 2026 10:51:47 +0200 Subject: [PATCH 1/2] Document stripping of no-op SQL CASTs Document https://github.com/dotnet/efcore/issues/36247 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../core/what-is-new/ef-core-11.0/whatsnew.md | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/entity-framework/core/what-is-new/ef-core-11.0/whatsnew.md b/entity-framework/core/what-is-new/ef-core-11.0/whatsnew.md index 78900f1671..c1394a52f5 100644 --- a/entity-framework/core/what-is-new/ef-core-11.0/whatsnew.md +++ b/entity-framework/core/what-is-new/ef-core-11.0/whatsnew.md @@ -164,6 +164,30 @@ Both optimizations can have a significant positive impact on query performance, 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. + + +### Stripping of no-op CASTs + +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. + +EF Core 11 now detects and strips these no-op `CAST` expressions during SQL post-processing. For example, the following query: + +```sql +SELECT [u].[Id], [u].[Name] +FROM [Users] AS [u] +WHERE CAST([u].[Name] AS nvarchar(max)) LIKE N'Name%' +``` + +Now generates the cleaner: + +```sql +SELECT [u].[Id], [u].[Name] +FROM [Users] AS [u] +WHERE [u].[Name] LIKE N'Name%' +``` + +This allows the database to use any index defined on the column, which can significantly improve query performance. + ### MaxBy and MinBy From 4cc0da8f2ffdf3238952eddec3cf854595470467 Mon Sep 17 00:00:00 2001 From: Shay Rojansky Date: Fri, 1 May 2026 09:39:25 +0200 Subject: [PATCH 2/2] Update entity-framework/core/what-is-new/ef-core-11.0/whatsnew.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- entity-framework/core/what-is-new/ef-core-11.0/whatsnew.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entity-framework/core/what-is-new/ef-core-11.0/whatsnew.md b/entity-framework/core/what-is-new/ef-core-11.0/whatsnew.md index c1394a52f5..3932a6f83b 100644 --- a/entity-framework/core/what-is-new/ef-core-11.0/whatsnew.md +++ b/entity-framework/core/what-is-new/ef-core-11.0/whatsnew.md @@ -178,7 +178,7 @@ FROM [Users] AS [u] WHERE CAST([u].[Name] AS nvarchar(max)) LIKE N'Name%' ``` -Now generates the cleaner: +Now generates cleaner SQL: ```sql SELECT [u].[Id], [u].[Name]