-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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: Allow PK with just the partition key and using id as the partition key #21396
Comments
This isn't a supported scenario. If the only property in the primary key is the partition key then the |
But what if most of our transactions are simply looking up user objects by id. It was my understanding that cosmos would be able to go directly to that partition and get that object instead of searching through every partition if the id I am using to search for is the partition key. The documentation also states that the partition key should be as unique as possible: Did I mis-interpret the doc? Also in the case where you say it is not supported, why throw an exception that doesn't tell you anything about the fact that this scenario is not supported? The exception gets thrown line 69 in https://github.com/dotnet/efcore/blob/master/src/EFCore.Cosmos/ValueGeneration/Internal/IdValueGenerator.cs It's also quite interesting that when I set the partition key but do not set the HasNoDiscriminator then the id field has the value of the Discriminator which means many items end up having the same Id which doesn't make sense to me. I propose either throwing an appropriate exception or just allowing the id field to contain the partition key. |
You read it correctly. It was updated since the last time I read it, we'll change our recommendation to match. |
When setting HasNoDiscriminator on the dbContext entity and the partitionkey and primary key are the same there is an error thrown when I try to add an entity to the context.
Steps to reproduce
My db context looks like this:
Notice the HasNoDiscriminator is added and the partition key is the primary key.
Then when I add to this DBContext like so:
var newAdmin = await _userDbContext.Users.AddAsync(admin, cancellationToken);
I get an exception with the following stack trace:
After looking at the code in https://github.com/dotnet/efcore/blob/master/src/EFCore.Cosmos/ValueGeneration/Internal/IdValueGenerator.cs
I believe I have found the issue. I will create a pr for it.
Further technical details
EF Core version: 3.1.5
Database provider: (e.g. Microsoft.EntityFrameworkCore.CosmosDb)
Target framework: 3.1(e.g. .NET Core 3.0)
Operating system: Windows 10 Enterprise
IDE: Visual Studio 2019 16.6
The text was updated successfully, but these errors were encountered: