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: Filter out the partition key property when it is the same as id #24873

Merged
merged 1 commit into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/EFCore.Cosmos/Metadata/Conventions/StoreKeyConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ private static void ProcessIdProperty(IConventionEntityTypeBuilder entityTypeBui
if (partitionKey != null)
{
var partitionKeyProperty = entityType.FindProperty(partitionKey);
if (partitionKeyProperty == null)
if (partitionKeyProperty == null
|| partitionKeyProperty == idProperty)
{
newKey = entityTypeBuilder.HasKey(new[] { idProperty })?.Metadata;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,28 @@ public virtual void No_alternate_key_is_created_if_primary_key_contains_id_and_p
Assert.Empty(entity.GetKeys().Where(k => k != entity.FindPrimaryKey()));
}

[ConditionalFact]
public virtual void No_alternate_key_is_created_if_id_is_partition_key()
{
var modelBuilder = CreateModelBuilder();

modelBuilder.Entity<Customer>().HasKey(nameof(Customer.AlternateKey));
modelBuilder.Entity<Customer>()
.Ignore(b => b.Details)
.Ignore(b => b.Orders)
.HasPartitionKey(b => b.AlternateKey)
.Property(b => b.AlternateKey).HasConversion<string>().ToJsonProperty("id");

var model = modelBuilder.FinalizeModel();

var entity = model.FindEntityType(typeof(Customer));

Assert.Equal(
new[] { nameof(Customer.AlternateKey) },
entity.FindPrimaryKey().Properties.Select(p => p.Name));
Assert.Empty(entity.GetKeys().Where(k => k != entity.FindPrimaryKey()));
}

protected override TestModelBuilder CreateModelBuilder()
=> CreateTestModelBuilder(CosmosTestHelpers.Instance);
}
Expand Down