Skip to content

Cosmos db Hierarchical Partitions in EF Core #28186

@Ntethelelom

Description

@Ntethelelom

Hi I have a question, when it comes to working with hierarchical partition keys in Entity Framework. The hierarchical partitions are defined in the cosmos database, but I am struggling to work with them in EF when I have to specify HasPartitionKey method. I get an error that my partition specified does not match the partition defined in the document. Please assist on how I can work with hierarchical partitions in EF. See code below:

modelBuilder.Entity(entity =>
{
entity.HasNoDiscriminator();
entity.ToContainer((Document));
entity.HasPartitionKey(x => new {x.ClientId , x.ProjectId} );
entity.HasKey(x => new { x.id });
});

Include stack traces

System.ArgumentException: 'The expression 'x => new <>f__AnonymousType1`2(ClientId = x.ClientId, ProjectId = x.ProjectId)' is not a valid member access expression. The expression should represent a simple property or field access: 't => t.MyProperty'. (Parameter 'memberAccessExpression')'

EF Core version: Microsoft.EntityFrameworkCore (6.0.5)
Database provider: Microsoft.EntityFrameworkCore.Cosmos (6.0.5)
Target framework: NET 6.0
IDE: Visual Studio 2022

Metadata

Metadata

Assignees

No one assigned
    No fields configured for Feature.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions