Pomelo.EntityFrameworkCore.MySql
Pomelo.EntityFrameworkCore.MySql copied to clipboard
Backslashes are not always escaped when String.Contains is translated to LIKE
Steps to reproduce
queryable.Where(e => e.StringProperty.Contains("\\"))
The issue
The escaping is wrong when using String.Contains("\\")
. It gets translated to e.StringProperty LIKE '%\\%'
(escaped only once, but should have been escaped twice for LIKE). Using String.Contains("\\%")
correctly translates to e.StringProperty LIKE '%\\\\\\%%'
.
By looking at the translator, it seems only strings with wildcard characters are escaped. https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql/blob/b02dd67febafcb9dc08b803175cf3039d8127804/src/EFCore.MySql/Query/ExpressionTranslators/Internal/MySqlStringComparisonMethodTranslator.cs#L479
\
must be escaped too. There is the same issue for String.StartsWith
and String.EndsWith
.
Further technical details
MySQL version: 10.9.8-MariaDB-1:10.9.8+maria~ubu2204 Operating system: Arch Linux, Alpine Linux 3.20, Fedora 40 Pomelo.EntityFrameworkCore.MySql version: 8.0.2 Microsoft.AspNetCore.App version: 8.0.8