Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cosmos: Extra clauses are removed from the query when extracting hierarchical partition key values #33960

Open
ajcvickers opened this issue Jun 11, 2024 · 0 comments

Comments

@ajcvickers
Copy link
Member

LINQ:

var sessions = await context.Set<UserSession>()
    .Where(
        e => e.Username.Contains("a")
             && e.TenantId == tenantId
             && e.UserId == userId
             && e.SessionId == sessionId)
    .ToListAsync();

Log:

info: 6/11/2024 09:52:53.942 CosmosEventId.ExecutingSqlQuery[30100] (Microsoft.EntityFrameworkCore.Database.Command) 
      Executing SQL query for container 'AppDbContext' in partition '["Microsoft","99a410d7-e467-4cc5-92de-148f3fc53f4c",7.0]' [Parameters=[]]
      SELECT c
      FROM root c
      WHERE (c["Discriminator"] = "UserSession")

Notice that the Contains clause is missing.

Repro
using (var context = new AppDbContext())
{
    await context.Database.EnsureDeletedAsync();
    await context.Database.EnsureCreatedAsync();

    var tenantId = "Microsoft";
    var sessionId = 7;
    var userId = new Guid("99A410D7-E467-4CC5-92DE-148F3FC53F4C");

    var sessions = await context.Set<UserSession>()
        .Where(
            e => e.Username.Contains("a")
                 && e.TenantId == tenantId
                 && e.UserId == userId
                 && e.SessionId == sessionId)
        .ToListAsync();
}

public class AppDbContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseCosmos(
                "https://localhost:8081",
                "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
                "EFDatabase")
            .LogTo(Console.WriteLine, LogLevel.Debug)
            .EnableSensitiveDataLogging();

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder
            .Entity<UserSession>()
            .HasPartitionKey(e => new { e.TenantId, e.UserId, e.SessionId });

        modelBuilder
            .Entity<UserSession>()
            .Property(e => e.Id).ValueGeneratedOnAdd();
    }
}

public class UserSession
{
    // Item ID
    public Guid Id { get; set; }

    // Partition Key
    public string TenantId { get; set; } = null!;
    public Guid UserId { get; set; }
    public int SessionId { get; set; }

    // Other members
    public string Username { get; set; } = null!;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant