question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

EF.Functions.Match translates to faulty sql

See original GitHub issue

Steps 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:closed
  • Created 2 years ago
  • Comments:10

github_iconTop GitHub Comments

1reaction
federicofz85commented, May 9, 2022

@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!

1reaction
lauxjpncommented, Feb 8, 2022

I’ve never seen this notation before. I would have expected that when passing object.Property on a string parameter, the value of the property would be passed, and then that the string value of the property would have been used as column name.

@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 an Expression object that has a Left property that is set to another expression representing your documentFile.Size property, a Right property that is set to another expression representing the constant value 42, and a NodeType property that is set to Add (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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found