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

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

Closed
benrhere opened this issue Jun 19, 2022 · 2 comments · Fixed by #33536
Closed

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

benrhere opened this issue Jun 19, 2022 · 2 comments · Fixed by #33536
Labels
area-cosmos closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-enhancement
Milestone

Comments

@benrhere
Copy link

benrhere commented Jun 19, 2022

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)

@CharlieAndLouise
Copy link

use this to avoid the error .HasConversion()

@benrhere
Copy link
Author

If I understand correctly, .HasConversion() will allow me to work with an integer type in my code, but will still require the underlying type in the document to be a string. I'd like to have the underlying type be an integer, which .HasConversion() doesn't solve.

ajcvickers added a commit that referenced this issue Apr 14, 2024
Fixes #28186
Fixes #28265
Fixes #33535

Also:
- Allow non-string partition key values
- Warn if no partition key has been defined
- Use a value converter if needed for values passed to WithPartitionKey (This is probably a bug, but should be docced as a breaking change anyway.)
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Apr 14, 2024
@ajcvickers ajcvickers modified the milestones: Backlog, 9.0.0 Apr 14, 2024
@ajcvickers ajcvickers self-assigned this Apr 14, 2024
ajcvickers added a commit that referenced this issue May 1, 2024
Fixes #28186
Fixes #28265
Fixes #33535

Also:
- Allow non-string partition key values
- Warn if no partition key has been defined
- Use a value converter if needed for values passed to WithPartitionKey (This is probably a bug, but should be docced as a breaking change anyway.)
ajcvickers added a commit that referenced this issue May 1, 2024
Fixes #28186
Fixes #28265
Fixes #33535

Also:
- Allow non-string partition key values
- Warn if no partition key has been defined
- Use a value converter if needed for values passed to WithPartitionKey (This is probably a bug, but should be docced as a breaking change anyway.)

Updated based on review feedback
@ajcvickers ajcvickers modified the milestones: 9.0.0, 9.0.0-preview5 May 2, 2024
@ajcvickers ajcvickers removed their assignment Aug 31, 2024
@roji roji modified the milestones: 9.0.0-preview5, 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 closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants