Search code examples
azure-cosmosdbentity-framework-core-3.0

CosmosDB - Entity Framework Core - Contains could not be translated


I try to manipulate a CosmosDB (SQL) using entity framework core 3.0 (Microsoft.EntityFrameworkCore.Cosmos 3.0.0).

Everything works fine except when I try to use Contains, StartWith, …

For example:

var query = _context.Challenges.Where(c => c.Name.Contains( "string"));

EF is supposed to translate it to the following SQL (that works perfectly on the CosmosDB – Query Explorer)

SELECT * FROM c WHERE CONTAINS(c.Name, "string")

But I receive the following error message:

The LINQ expression 'Where<Challenge>(\n    source: DbSet<Challenge>, \n    predicate: (c) => c.Name.Contains(\"string\"))' 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 either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). 
See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

Of course, I don’t want to code it like the following, that will execute the entire contains on the client, just to make a simple LIKE…

List<Challenge> entities = _context.Challenges.AsEnumerable().Where(c => c.Name.Contains( "string")).ToList();

Anyone has an idea of to evaluate the "contains" on the server side ?

Note: I try the exact same code using UseSqlServer rather than UseCosmos (and by adding the needed [Key] annotation and creating a SQL server) and it works like a charm.... So it's a definitively a CosmosDB vs EF issue.


Solution

  • By posting the same issue on entity framework core forum (https://github.com/aspnet/EntityFrameworkCore/), the answer was that this feature is not yet implemented:

    https://github.com/aspnet/EntityFrameworkCore/issues/18673

    https://github.com/aspnet/EntityFrameworkCore/issues/16143

    https://github.com/aspnet/EntityFrameworkCore/issues/18451

    https://feedback.azure.com/forums/263030-azure-cosmos-db/suggestions/6333414-implement-like-keyword