Skip to content

Commit

Permalink
Filter out the partition key property when it is the same as id
Browse files Browse the repository at this point in the history
Fixes #24872
  • Loading branch information
AndriySvyryd committed May 10, 2021
1 parent fd599b2 commit 7249f62
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/EFCore.Cosmos/Metadata/Conventions/StoreKeyConvention.cs
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
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

0 comments on commit 7249f62

Please sign in to comment.