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

Closed
ajcvickers opened this issue Jun 11, 2024 · 0 comments · Fixed by #34187
Assignees
Labels
area-cosmos area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

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!;
}
@ajcvickers ajcvickers self-assigned this Jun 11, 2024
@ajcvickers ajcvickers added this to the 9.0.0 milestone Jun 11, 2024
@roji roji assigned roji and unassigned ajcvickers Jul 6, 2024
@roji roji added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jul 6, 2024
roji added a commit to roji/efcore that referenced this issue Jul 8, 2024
roji added a commit to roji/efcore that referenced this issue Jul 8, 2024
roji added a commit to roji/efcore that referenced this issue Jul 8, 2024
roji added a commit to roji/efcore that referenced this issue Jul 8, 2024
roji added a commit to roji/efcore that referenced this issue Jul 8, 2024
roji added a commit to roji/efcore that referenced this issue Jul 8, 2024
@roji roji closed this as completed in 041f4f2 Jul 9, 2024
@ajcvickers ajcvickers modified the milestones: 9.0.0, 9.0.0-preview7 Aug 21, 2024
@roji roji modified the milestones: 9.0.0-preview7, 9.0.0 Oct 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-cosmos area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants