Skip to content

EFCore 3.1 + CosmosDb can't translate any() on owned object #20441

Description

@darthg8r

I have a query that seems to be pretty basic. I'm using Core3.1 on top of CosmosDb. I simply want to query a list of orders that have order details, but I'm getting this error:

.Where(b => EF.Property>(b, "OrderDetails") .Any())' could not be translated. Either rewrite the query in a form that can be translated or switch to client evaluation.

Client evaluation isn't an option

Consider this contrived example:

public class Order
{
    public List<OrderDetail> OrderDetails {get;set;}
}

public class OrderDetail
{

}

// Db Context
modelBuilder.Entity<Order>().OwnsMany(x => x.OrderDetails);


// The query
context.Orders.Where( order => order.OrderDetails.Any());

I do this in my sleep with SQL. What gives?

Activity

  1. smitpatel commented on Mar 27, 2020

    @smitpatel
    Contributor

    Can you provide us what would be expected SQL server side to get the same results?

  2. darthg8r commented on Mar 28, 2020

    @darthg8r
    Author

    If this were on SQL server instead of Cosmos, you could use this to get the orders that have OrderDetails

    select * from Orders
    where id in (
    select distinct OrderId from OrderDetails
    )

    Or

    Select * from Orders
    inner join OrderDetails on Order.Id = OrderDetails.OrderId

  3. smitpatel commented on Mar 28, 2020

    @smitpatel
    Contributor

    If this were on SQL server instead of Cosmos

    We are talking about Cosmos translation. Cosmos does not run all the SQL, SqlServer can run.

  4. darthg8r commented on Mar 28, 2020

    @darthg8r
    Author

    It's surely possible to get a list of objects that has more than one owned object. Right? Seems pretty basic. Mongo can do it.

  5. smitpatel commented on Mar 28, 2020

    @smitpatel
    Contributor

    Surely you can use mongo then.

  6. darthg8r commented on Mar 28, 2020

    @darthg8r
    Author

    Is that your passive aggressive way of saying no, it doesn't support that?

  7. smitpatel commented on Mar 28, 2020

    @smitpatel
    Contributor

    It is pretty clear fact that EF Core 3.1 does not support that hence you got an exception. If we want to make it working in future version of EF Core then we need a way to represent that in SQL. I don't think discussing about other databases and their capabilities answer that question.

  8. darthg8r commented on Mar 28, 2020

    @darthg8r
    Author

    It's also pretty clear what I'm trying to accomplish. Instead of passive aggressive, you could have offered a workaround, or a simple, I'm sorry, we don't support querying objects that are owned. The mongodb comment was quite valid considering that you can talk mongo protocol to CosmosDb. I would be using it, but that would mean losing EFCore.

  9. smitpatel commented on Mar 28, 2020

    @smitpatel
    Contributor

    There is no work-around else I would have offered you one. While CosmosDb internally use same engine for Azure SQL and mongodb, the protocol to talk to each API is different. Just because CosmosDb mongodb API can do something does not necessarily mean there is SQL equivalent.

  10. darthg8r commented on Mar 28, 2020

    @darthg8r
    Author

    Ok, here's working SQL
    select * from c where ARRAY_LENGTH(c.OrderDetails) > 0

  11. added this to the Backlog milestone on Mar 30, 2020
  12. benabdalla-oussama commented on Jun 9, 2020

    @benabdalla-oussama

    Hello everyone,

    I've got the same issue.
    These are my classes.

    public class ArticleEntity
    {
       public Guid Id { get; set; }
       [Required, StringLength(1000)]
       public string Title { get; set; }
       
       public ICollection<ArticleTagEntity> Tags { get; set; }
    }
    
    public class ArticleTagEntity
    {
       public Guid TagId { get; set; }
    }

    and this is my DB context,

    public DbSet<ArticleEntity> Articles { get; set; }
    
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<ArticleEntity>().ToContainer("Articles").Property(p => p.Id).ValueGeneratedOnAdd();
        modelBuilder.Entity<ArticleEntity>().OwnsMany<ArticleTagEntity>(p=> p.Tags);
    }

    we are using the above classes and we use Odata URL for retrieving, see example below:

    https://localhost:44326/odata/articles?$filter=Tags/any(c: c/TagId eq 9c60ad92-bd94-4e78-9ece-3a583e7a1004)

    this creates the error below:

    InvalidOperationException: The LINQ expression 'DbSet<ArticleEntity>()
    .Where(a => EF.Property<ICollection<ArticleTagEntity>>(a, "Tags")
    .AsQueryable()
    .Any(o => o.TagId == 9c60ad92-bd94-4e78-9ece-3a583e7a1004))' 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.
    

    Also when we do this in LINQ code like below it gives the same error:

    Context.Articles.Where(x => x.Tags.Any(i => i.TagId == Guid.Parse("9c60ad92-bd94-4e78-9ece-3a583e7a1004"))).ToList();
    

    This is a test situation, I think this should be supported.

    Thank you in advance,
    Oussama.

  13. 15 remaining items

  14. added this to the 9.0.0 milestone on Jun 15, 2024
  15. added 4 commits that reference this issue on Jun 15, 2024
    7a85df8
    39f890e
    92f348b
    f4c0b07
  16. added a commit that references this issue on Jun 20, 2024
    531c238
  17. modified the milestones: 9.0.0, on Jun 21, 2024
  18. modified the milestones: , 9.0.0 on Oct 12, 2024
  19. 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

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions