You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have created a DBFunctions / IMethodCallTranslator extension called FreeTextSearch (The idea being that it provides a wrapper round the SQLite free text search Match method).
The prototype for the extension is in DbFunctionsExtensions.cs:
public static bool FreeTextSearch(this DbFunctions _, object propertyReference, string freeText, [NotParameterized]IFreeTextConfig config) => throw new NotImplementedException("This method is for use with Entity Framework Core only and has no in-memory implementation.");
When the extension is called like this (in Tests.cs):
The translator fails to get called and the an InvalidOperationException with the text shown below is raised:
System.InvalidOperationException: 'The LINQ expression 'DbSet()
.Where(t => __Functions_0
.FreeTextSearch(
propertyReference: t.TextContent,
freeText: "MIKE",
config: (IFreeTextConfig)SqliteFreeTextConfig))' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.'
However, when I change IFreeTextConfig to SqliteFreeTextConfig (see below) then it works fine and the translator code gets called.
The argument config in the method FreeTextSearch is type IFreeTextConfig so why does it not work if I pass an interface but does when I pass a concrete class?
Attached is a project that can be used to demonstrate the issue.
This is likely because in the expression tree, a Convert node is introduced to cast up from SqliteFreeTextConfig to IFreeTextConfig, and that blocks usage of the IMethodTranslator (since it's a complex fragment etc). We can look into handling more Convert node scenarios, but this is a quite niche and we're very unlikely to get around to any time soon - I'm placing this in the backlog.
In the meantime, avoid using interfaces here and just pass the concrete type.
Bug description
I have created a DBFunctions / IMethodCallTranslator extension called FreeTextSearch (The idea being that it provides a wrapper round the SQLite free text search Match method).
The prototype for the extension is in DbFunctionsExtensions.cs:
public static bool FreeTextSearch(this DbFunctions _, object propertyReference, string freeText, [NotParameterized]IFreeTextConfig config) => throw new NotImplementedException("This method is for use with Entity Framework Core only and has no in-memory implementation.");
When the extension is called like this (in Tests.cs):
IFreeTextConfig config = new SqliteFreeTextConfig(); IQueryable<TestTableDto> query = context.TestTable.Where(e => EF.Functions.FreeTextSearch(e.TextContent!, "MIKE", config));
The translator fails to get called and the an InvalidOperationException with the text shown below is raised:
System.InvalidOperationException: 'The LINQ expression 'DbSet()
.Where(t => __Functions_0
.FreeTextSearch(
propertyReference: t.TextContent,
freeText: "MIKE",
config: (IFreeTextConfig)SqliteFreeTextConfig))' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.'
However, when I change IFreeTextConfig to SqliteFreeTextConfig (see below) then it works fine and the translator code gets called.
SqliteFreeTextConfig config = new SqliteFreeTextConfig(); IQueryable<TestTableDto> query = context.TestTable.Where(e => EF.Functions.FreeTextSearch(e.TextContent!, "MIKE", config));
The argument
config
in the methodFreeTextSearch
is typeIFreeTextConfig
so why does it not work if I pass an interface but does when I pass a concrete class?Attached is a project that can be used to demonstrate the issue.
Thanks,
Mike
EntityFrameworkTest.zip
Your code
.
Stack traces
Verbose output
EF Core version
9.0.2
Database provider
Microsoft.EntityFrameworkCore.Sqlite
Target framework
.Net 9.0
Operating system
Windows 11
IDE
Visual Studio 2022 17.13.2
The text was updated successfully, but these errors were encountered: