efcore
efcore copied to clipboard
Cosmos query does not extract and use partial hierarchical partition key
Query, where Region is the top-level partition key:
var customers = await context
.Set<Customer>()
.Where(e => e.Id == id && e.Region == region)
.ToListAsync();
SQL:
info: 8/28/2024 12:14:24.244 CosmosEventId.ExecutedReadNext[30102] (Microsoft.EntityFrameworkCore.Database.Command)
Executed ReadNext (260.3355 ms, 2.79 RU) ActivityId='206d1672-4891-47f3-a378-742711a1261b', Container='SomeDbContext', Partition='None', Parameters=[@__id_0='0e2ba07d-e248-42a7-9ba2-08dcc6ad40f4', @__region_1='Europe']
SELECT VALUE c
FROM root c
WHERE ((c["Id"] = @__id_0) AND (c["Region"] = @__region_1))
Repro
using (var context = new SomeDbContext())
{
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
var id = new Guid("0e2ba07d-e248-42a7-9ba2-08dcc6ad40f4");
var region = "Europe";
var country = "UK";
var customers = await context
.Set<Customer>()
.Where(e => e.Id == id && e.Region == region)
.ToListAsync();
}
public class SomeDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseCosmos(
"https://localhost:8081",
"C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
"EFDatabase")
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();
protected override void OnModelCreating(ModelBuilder modelBuilder)
=> modelBuilder
.Entity<Customer>()
.HasPartitionKey(e => new { e.Region, e.PrimaryCountry });
}
public class Customer
{
public Guid Id { get; set; }
public required string Name { get; set; }
public required string PrimaryCountry { get; set; }
public required string Region { get; set; }
}