Cosmos: allow value converters for collections of primitive types
See https://github.com/npgsql/efcore.pg/blob/main/src/EFCore.PG/Storage/ValueConversion/INpgsqlArrayConverter.cs for a way of implementing this.
Also try to use built-in converters automatically, see https://github.com/npgsql/efcore.pg/blob/main/src/EFCore.PG/Storage/ValueConversion/NpgsqlValueConverterSelector.cs
Note from triage: also consider other issues related to primitive collections--see label area-primitive-collections.
Note for team: either a duplicate of #4179, or already supported if the issue means using value converters explicitly.
Related to #34026
See #36330 for failing Contains over parameterized scalar collection of value-converted elements.
Minimal repro
await using var context = new BlogContext();
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
List<ThingId> list = [new("a"), new("b")];
// this throws an exception:
// System.InvalidOperationException: Couldn't find array type mapping when applying item/array mappings
var results = await context.Things.Where(x => list.Contains(x.Id)).ToListAsync();
Console.WriteLine(results.Count);
public class BlogContext : DbContext
{
public DbSet<Thing> Things { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseCosmos(
Environment.GetEnvironmentVariable("CosmosNoSql__ConnectionString")!,
databaseName: "test",
o => o
.ConnectionMode(ConnectionMode.Gateway)
.HttpClientFactory(() => new HttpClient(
new HttpClientHandler
{
ServerCertificateCustomValidationCallback =
HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
})))
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Thing>().Property(x => x.Id).HasConversion(v => v.Value, v => new ThingId(v));
}
}
public record Thing
{
public ThingId Id { get; set; }
public string Name { get; set; }
}
public record ThingId(string Value);