Skip to content

Why can't a CosmosDB partition key be an int when working with EF Core? #28265

@benrhere

Description

@benrhere

Ask a question

As you can see here:

if (partitionKeyType != typeof(string))
{
throw new InvalidOperationException(
CosmosStrings.PartitionKeyNonStringStoreType(
partitionKeyPropertyName, entityType.DisplayName(), partitionKeyType.ShortDisplayName()));
}

EF Core Cosmos provider seems to force that a partition key be a string (or have a value converter to map to a string). But CosmosDB itself does not seem to have this restriction. According to: https://docs.microsoft.com/en-us/azure/cosmos-db/partitioning-overview :

"The partition key value can be of string or numeric types."

I've confirmed that I can successfully use int as a Cosmos DB document partition key type (when EF Core is not in the picture). But Cosmos seems to throw the exception outlined in the above github link whenever you use WithPartitionKey for the relevant document:

"System.InvalidOperationException: 'The type of the partition key property [property] on [document] is 'int'. All partition key properties need to be strings or have a string value converter.'"

It seems like I should be able to specify an integer partition key when using EF Core with Cosmos DB. Am I mistaken?

Include provider and version information

EF Core version: 6.0.6
Database provider: (e.g. Microsoft.EntityFrameworkCore.Cosmos)
Target framework: (e.g. .NET 6.0)
Operating system:
IDE: (e.g. Visual Studio 2022 17.2.4)

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