EF.Functions.Match translates to faulty sql
See original GitHub issueSteps to reproduce
Standard asp.net core mvc application.
Entity Class
public class DocumentFileEntity : BaseEntity
{
...
public String? Text { get; set; }
...
}
The issue
Using the Function EF.Functions.Match
in a Linq-Where Statement leads to non executable SQL.
The used Linq-Query is:
List<DocumentEntity> documentEntities = await this.dbContext.DocumentFiles
.Where(documentFile => EF.Functions.Match("Text", searchTerm, MySqlMatchSearchMode.NaturalLanguage))
.Select(documentFile => documentFile.Document)
.ToListAsync();
Generated SQL and Exception
fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
Failed executing DbCommand (5ms) [Parameters=[@__searchTerm_1='?' (Size = 4000)], CommandType='Text', CommandTimeout='30']
SELECT `d0`.`Id`, `d0`.`FolderId`, `d0`.`Name`, `d0`.`Type`
FROM `DocumentFiles` AS `d`
INNER JOIN `Documents` AS `d0` ON `d`.`DocumentId` = `d0`.`Id`
WHERE MATCH ('Text') AGAINST (@__searchTerm_1)
fail: Microsoft.EntityFrameworkCore.Query[10100]
An exception occurred while iterating over the results of a query for context type 'Flek.Data.ApplicationDbContext'.
MySqlConnector.MySqlException (0x80004005): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''Text') AGAINST ('a')' at line 4
It looks to me like the single quotes around the column name ‘Text’ name are the problem. A related PR that i found is #1268.
Further technical details
Pomelo.EntityFrameworkCore.MySql version: 6.0.1 Microsoft.AspNetCore.App version: .NET 6
Issue Analytics
- State:
- Created 2 years ago
- Comments:10
Top Results From Across the Web
Entity framework EF.Functions.Like vs string.Contains
EF already knows how to translate string.Contains / StartsWith to the corresponding SQL operations, doesn't it? The only reason i can think of ......
Read more >Efficient Querying - EF Core
Performance guide for efficient querying using Entity Framework Core. ... Matches on start, so uses an index (on SQL Server) var posts1 ...
Read more >Breaking changes in EF Core 5.0
Complete list of breaking changes introduced in Entity Framework Core 5.0. ... decimal values, thereby matching the SQL Server behavior.
Read more >EF Core case insensitive database search : r/csharp
The main problem is that EF eventually turns your queries into SQL statements. That means the case sensitivity is decided by your database....
Read more >Translations | Npgsql Documentation
Entity Framework Core allows providers to translate query expressions to SQL for database ... EF.Functions.Like(matchExpression, pattern, escapeCharacter) ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@lauxjpn Hi guys is there a way to match with 2 or more properties? I noticed that there’s a overload that asks for string[] properties but I can’t make it work. I already have the index but I’m getting System.InvalidOperationException: Expression ‘@__test_3’ in the SQL tree does not have a type mapping assigned.
Found it! new[] { x.Col1, x.Col2, x.Col3 } Thanks!
@RSBlek No problem. Your EF Core LINQ queries are actually not C# code that is being executed. Instead, they are expression trees that describe the operations that you have typed.
If e.g. you have a
documentFile.Size + 42
operation in your LINQ query (for whatever reason), then EF Core and Pomelo will receive anExpression
object that has aLeft
property that is set to another expression representing yourdocumentFile.Size
property, aRight
property that is set to another expression representing the constant value42
, and aNodeType
property that is set toAdd
(which is the operation that should be applied to the two properties/operands).So what EF Core and Pomelo do, is basically just to parse this expression tree and convert all the expressions to SQL in a way that makes sense.
See Expression Trees (C#) for more general information.