Skip to content

Escape the LIKE escape character for char parameters in StartsWith/EndsWith/Contains - #39316

Merged
AndriySvyryd merged 1 commit into
dotnet:mainfrom
ilkertskn:fix/like-escape-char-parameter
Oct 21, 2026
Merged

AndriySvyryd merged 1 commit into
dotnet:mainfrom
ilkertskn:fix/like-escape-char-parameter

Conversation

@ilkertskn

Copy link
Copy Markdown
Contributor

For a char parameter, SQL Server and SQLite build the LIKE pattern at runtime and always add ESCAPE '', but only the wildcard characters were escaped, not the escape character itself. With a backslash parameter, StartsWith produced '%' (matches only the string "%"), EndsWith produced '%' (a dangling escape that matches nothing) and SQL Server's Contains produced '%%%' (matches any string containing '%'). String parameters were already escaped correctly.

  • [x ] I've read the guidelines for contributing and seen the walkthrough
  • [x ] I've posted a comment on an issue with a detailed description of how I am planning to contribute and got approval from a member of the team
  • [x ] The code builds and tests pass locally (also verified by our automated build checks)
  • [x ] Commit messages follow this format:
        Summary of the changes
        - Detail 1
        - Detail 2

        Fixes #bugnumber
  • [ x] Tests for the changes have been added (for bug fixes / features)
  • Code follows the same patterns and style as existing code in this repo

…dsWith/Contains

For a char parameter, SQL Server and SQLite build the LIKE pattern at runtime
and always add ESCAPE '\', but only the wildcard characters were escaped, not
the escape character itself. With a backslash parameter, StartsWith produced
'\%' (matches only the string "%"), EndsWith produced '%\' (a dangling escape
that matches nothing) and SQL Server's Contains produced '%\%%' (matches any
string containing '%'). String parameters were already escaped correctly.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The reviewed changes address the reported escaping issue and include shared regression coverage with provider-specific baselines.

No open issues 🎉

What changed in this PR

This pull request fixes LIKE pattern construction for backslash character parameters in SQL Server and SQLite StartsWith, EndsWith, and Contains translations.

Changes:

  • Escape the LIKE escape character in runtime patterns.
  • Add shared regression coverage for all three string operations.
  • Add SQL Server and SQLite baseline assertions.
File Description
test/​EFCore.SqlServer.FunctionalTests/​Query/​FunkyDataQuerySqlServerTest.cs Adds SQL Server baselines for corrected runtime patterns.
test/​EFCore.Sqlite.FunctionalTests/​Query/​FunkyDataQuerySqliteTest.cs Adds SQLite baselines for corrected runtime patterns.
test/​EFCore.Specification.Tests/​Query/​FunkyDataQueryTestBase.cs Adds shared regression tests for backslash character parameters.
src/​EFCore.SqlServer/​Query/​Internal/​SqlServerSqlTranslatingExpressionVisitor.cs Escapes backslash character parameters in SQL Server LIKE patterns.
src/​EFCore.Sqlite.Core/​Query/​Internal/​SqliteSqlTranslatingExpressionVisitor.cs Escapes backslash character parameters in SQLite LIKE patterns.

Review effort: Lite

@AndriySvyryd
AndriySvyryd merged commit 649620c into dotnet:main Oct 21, 2026
20 checks passed
@AndriySvyryd

Copy link
Copy Markdown
Member

Thanks for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants