dotnet / dotnet/efcore

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

Open
#35,092 4 comments 4 reactions 1 assignee Claimed by @AndriySvyryd View on GitHub
area-cosmos area-query customer-reported regression
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

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
```csharp
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
{
public void Configure(EntityTypeBuilder builder)
{
builder.HasDiscriminator(x => x.Type)
.HasValue(ProductType.Regular)
.HasValue(ProductType.Supplement)
.HasValue(ProductType.Menu);

builder.Property(x => x.Type)
.HasConversion();
// 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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.