On EF.core 8.x with mariaDB I have problems querying into JSON columns using LINQ syntax.
The table Analyses contains a JSON column called Data
This simple query
var list = dbContext.Analyses.Include(a => a.Site)
.Where(a => a.Data.Count > 0)
.ToList();
fails with
System.InvalidOperationException: The LINQ expression 'DbSet<Analysis>()
.Count(a => a.Data.Count > 0)' 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'.
I've added Pomelo.EntityFrameworkCore.MySql.Json.Microsoft 8.0.2 and added builder options:
optionsBuilder.UseMySql(options.ConnectionString, GetServerVersion(), builderOptions => builderOptions.UseMicrosoftJson());
Now the query above does no longer fail but the resulting query returns 0 rows:
SELECT `a`.`Id`, `a`.`Data`, `a`.`LastAnalysisDateTime`, `a`.`Result`, `a`.`SiteId`, `s`.`Id`, `s`.`Domain`, `s`.`LastResolvedDateTime`, `s`.`ResolvedDomain`
FROM `Analyses` AS `a`
INNER JOIN `Sites` AS `s` ON `a`.`SiteId` = `s`.`Id`
WHERE CAST(JSON_EXTRACT(`a`.`Data`, '$.Count') AS signed) > 0
Trying this query in a db management tool yields the same result. The JSON_EXTRACT thing with the JSON selector $.Count seem to be the wrong approach.
There is so little documentation about the EF.core with JSON support for mariadb that I actually have no ideas on how to fix this.
According to maria db docs the where clause (which looks overly complicated to me) can be easily written as
WHERE JSON_LENGTH(`a`.`Data`) > 0
(which works correctly).
But I see no good way of implementing this beside using raw db queries, skipping entirely the hydration (the creation of tracked EF object) and effectively by-passing EF.
Did somebody else had any success with EF.core, JSON and mariadb?