Skip to content

Cosmos SQL query enum array (Contains) to string conversion not working properly #35092

Description

@Kevenvz

I have an issue with the .Contains query translation in Cosmos.
The problem is as follows:
My entity has an enum discriminator type, I want to serialize this to a string for better readability. Now when I want to apply .Contains in my query the enum gets serialized as an array of integers instead of an array of strings.

The logs show the following query

info: Microsoft.EntityFrameworkCore.Database.Command[30100]
Executing SQL query for container 'Products' in partition 'None' [Parameters=[@__types_0='[1,2]']]
SELECT VALUE c
FROM root c
WHERE (c["Type"] IN ("Menu", "Regular", "Supplement") AND ARRAY_CONTAINS(@__types_0, c["Type"]))

Snippet of the related code

public enum ProductType
{
    Supplement,
    Menu,
    Regular
}

public abstract class Product
{
    public ProductType Type { get; init; }
}

public class MenuProduct : Product
{
    public MenuProduct()
    {
        Type = ProductType.Menu;
    }
}

public class Supplement : Product
{
    public MenuProduct()
    {
        Type = ProductType.Supplement;
    }
}

public class RegularProduct : Product
{
    public RegularProduct()
    {
        Type = ProductType.Regular;
    }
}

public class ProductConfiguration : IEntityTypeConfiguration<Product>
{
    public void Configure(EntityTypeBuilder<Product> builder)
    {
        builder.HasDiscriminator(x => x.Type)
            .HasValue<RegularProduct>(ProductType.Regular)
            .HasValue<Supplement>(ProductType.Supplement)
            .HasValue<MenuProduct>(ProductType.Menu);

        builder.Property(x => x.Type)
            .HasConversion<string>();
        // Rest is left out
    }
}

ProductType[] types = [ProductType.Menu, ProductType.Regular];
Product[] results = await dbContext.Products
    .Where(p => types.Contains(p.Type))
    .ToArrayAsync();

EF Core version: 9.0.0
Database provider: Microsoft.EntityFrameworkCore.Cosmos
Target framework: .NET 9
Operating system: Windows and Linux (Docker)
IDE: JetBrains Rider 2024.2.6

Metadata

Metadata

Assignees

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions