diff --git a/Microsoft.Azure.Cosmos/src/Resource/Settings/ContainerProperties.cs b/Microsoft.Azure.Cosmos/src/Resource/Settings/ContainerProperties.cs index e1eaa6ca96..b95efb99c8 100644 --- a/Microsoft.Azure.Cosmos/src/Resource/Settings/ContainerProperties.cs +++ b/Microsoft.Azure.Cosmos/src/Resource/Settings/ContainerProperties.cs @@ -362,10 +362,17 @@ public string PartitionKeyPath throw new ArgumentNullException(nameof(this.PartitionKeyPath)); } + PartitionKeyDefinitionVersion? currentDefinitionVersion = this.PartitionKeyDefinitionVersion; + this.PartitionKey = new PartitionKeyDefinition { Paths = new Collection() { value } }; + + if (currentDefinitionVersion.HasValue) + { + this.PartitionKeyDefinitionVersion = currentDefinitionVersion.Value; + } } } diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/CosmosContainerSettingsTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/CosmosContainerSettingsTests.cs index 84618705de..865bb71d18 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/CosmosContainerSettingsTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/CosmosContainerSettingsTests.cs @@ -165,6 +165,17 @@ public void ValidateIncludedPathSerialization() } } + [TestMethod] + public void SettingPKShouldNotResetVersion() + { + ContainerProperties containerProperties = new(); + containerProperties.Id = "test"; + containerProperties.PartitionKeyDefinitionVersion = Cosmos.PartitionKeyDefinitionVersion.V2; + containerProperties.PartitionKeyPath = "/id"; + + Assert.AreEqual(Cosmos.PartitionKeyDefinitionVersion.V2, containerProperties.PartitionKeyDefinitionVersion); + } + private static string SerializeDocumentCollection(DocumentCollection collection) { using (MemoryStream ms = new MemoryStream())