0

I am trying to create a cosmos db partition using the below sample. I am using managed identity to authenticate my requests. I am using Azure.ResourceManager v1.3.1 and Azure.ResourceManager.CosmosDB v1.0.1

While trying to run below snippet, I am getting:

Azure.RequestFailedException: 'Message: {"code":"BadRequest","message":"Message: {"Errors": ["A Partition key definition is not specified in the request."]}

I am using .NET Framework 4.7.2

I am not sure how we to pass the partition key definition while creating a cosmos DB SQL Container. Can anyone please help here. Thanks.

    private static async Task TestStub1()
    {
        var subscriptionId = "xxx";
        var resourceGroupName = "xxx";
        var accountName = "xxx";
        var databaseName = "xxx";
        var containerName = "xxx";
        var throughputProperties = ThroughputProperties.CreateAutoscaleThroughput(4000);
        var accountEndpoint = "xxx";
        var location = AzureLocation.EastUS;
        try
        {
            var tokenCredential = new DefaultAzureCredential();
            var armClient = new ArmClient(tokenCredential);

            var dbAccountIdentifier = new ResourceIdentifier($"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DocumentDB/databaseAccounts/{accountName}");

            var dbAccount = armClient.GetCosmosDBAccountResource(dbAccountIdentifier);
            var databases = dbAccount.GetCosmosDBSqlDatabases();

            var cosmosDBSqlDatabaseResourceInfo = new CosmosDBSqlDatabaseResourceInfo(databaseName);
            var cosmosDBSqlDatabaseCreateOrUpdateContent = new CosmosDBSqlDatabaseCreateOrUpdateContent(location, cosmosDBSqlDatabaseResourceInfo);
            var cosmosDBSqlResource = await databases.CreateOrUpdateAsync(Azure.WaitUntil.Completed, databaseName, cosmosDBSqlDatabaseCreateOrUpdateContent);
            if (cosmosDBSqlResource.HasValue)
            {
                var cosmosDBSqlContainers = cosmosDBSqlResource.Value.GetCosmosDBSqlContainers();

                var cosmosDBContainerPartitionKey = new CosmosDBContainerPartitionKey();
                cosmosDBContainerPartitionKey.Kind = CosmosDBPartitionKind.Hash;

                var cosmosDBSqlContainerResourceInfo = new CosmosDBSqlContainerResourceInfo(containerName);
                cosmosDBSqlContainerResourceInfo.PartitionKey = cosmosDBContainerPartitionKey;
                
                var cosmosDBSqlContainerCreateOrUpdateContent = new CosmosDBSqlContainerCreateOrUpdateContent(location, cosmosDBSqlContainerResourceInfo);
                var cosmosDBSqlContainer = await cosmosDBSqlContainers.CreateOrUpdateAsync(WaitUntil.Completed, containerName, cosmosDBSqlContainerCreateOrUpdateContent);
            }
        }
        catch (Exception ex)
        {
            throw;
        }
    }
Vaibhav Singla
  • 832
  • 1
  • 10
  • 14

1 Answers1

0

This is a sample for Microsoft.Azure.Management.CosmosDB but should be the same.

Assuming a string parameter of partitionKey this is how you create a partition key definition.

PartitionKey = new ContainerPartitionKey
{
   Kind = "Hash",
   Paths = new List<string> { partitionKey },
   Version = 1 //version 2 for large partition key
},

More samples like this at Azure Management Libraries for .NET for Azure Cosmos DB

Mark Brown
  • 8,113
  • 2
  • 17
  • 21