Skip to content

CosmosDB - Entity Framework Core - Contains could not be translated #18673

Closed

Description

@patrickdgt

I try to manipulate a CosmosDB (SQL) using entity framework core 3.0. Everything works fine except when I try to use Contains

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(\n source: DbSet, \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 entities = _context.Challenges.AsEnumerable().Where(c => c.Name.Contains( "string")).ToList();

Any 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 I think it's a definitively a CosmosDB vs EF issue.

EF Core version:
Database provider: Microsoft.EntityFrameworkCore.Cosmos
Target framework: NET Core 3.0.0
Operating system: OS X Windows 10 (try both)
IDE: Last stable version on both OSX and Windows 10

Thanks in advance for your help.

Activity

  1. smitpatel commented on Oct 31, 2019

    @smitpatel
    Contributor

    Duplicate of #16143

  2. added this to the Backlog milestone on Nov 1, 2019
  3. ajcvickers commented on Nov 1, 2019

    @ajcvickers
    Contributor

    Note from triage: do the 3.1 relational fix for Cosmos.

  4. krw1010 commented on Jan 10, 2020

    @krw1010

    Any further info on this? I'm encountering the same error in .NET Core 3.1. @ajcvickers What is the relational fix you mentioned?

    var user = await Context.Users.Where(x => x.email.Contains("string")).FirstOrDefaultAsync();

    System.InvalidOperationException
    HResult=0x80131509
    Message=The LINQ expression 'DbSet
    .Where(a => a.Email.Contains(__search_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 either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
    Source=Microsoft.EntityFrameworkCore

  5. xtellurian commented on Jan 21, 2020

    @xtellurian

    I'm also seeing this error in 3.1

  6. nickwilsonr commented on Jan 21, 2020

    @nickwilsonr

    Still an issue in 3.1.1.. It looks like the solution has been merged though, so when can we expect it to be released?

  7. ajcvickers commented on Jan 22, 2020

    @ajcvickers
    Contributor

    @AndriySvyryd @smitpatel Difference between #16143 and this issue?

  8. AndriySvyryd commented on Jan 22, 2020

    @AndriySvyryd
    Member

    This is a subset.
    Fixed in 83dccdc

  9. modified the milestones: Backlog, 5.0.0 on Jan 22, 2020
  10. modified the milestones: 5.0.0, on Mar 13, 2020
  11. modified the milestones: , 5.0.0 on Nov 7, 2020
  12. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions