You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Azure.ResourceManager.CosmosDB 1.0.1创建SQL容器时如何指定分区键?

解决Cosmos DB SQL容器创建时“请求中未指定分区键定义”错误

问题背景

使用.NET Framework 4.7.2,通过托管身份认证,借助Azure.ResourceManager v1.3.1和Azure.ResourceManager.CosmosDB v1.0.1创建Cosmos DB SQL容器时,触发Azure.RequestFailedException,错误提示“请求中未指定分区键定义”。

错误原因

代码中仅设置了CosmosDBContainerPartitionKey的Kind属性,但未指定分区键路径(Paths)——这是创建容器时的必填参数,Azure需要明确知道数据按哪个字段进行分区。

修正后的代码

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 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
            {
                Kind = CosmosDBPartitionKind.Hash,
                Paths = new List<string> { "/myPartitionKey" } // 替换为你的分区键实际路径
            };

            var cosmosDBSqlContainerResourceInfo = new CosmosDBSqlContainerResourceInfo(containerName)
            {
                PartitionKey = cosmosDBContainerPartitionKey,
                Throughput = throughputProperties // 可选:添加吞吐量配置
            };
            
            var cosmosDBSqlContainerCreateOrUpdateContent = new CosmosDBSqlContainerCreateOrUpdateContent(location, cosmosDBSqlContainerResourceInfo);
            var cosmosDBSqlContainer = await cosmosDBSqlContainers.CreateOrUpdateAsync(WaitUntil.Completed, containerName, cosmosDBSqlContainerCreateOrUpdateContent);
        }
    }
    catch (Exception ex)
    {
        throw;
    }
}

关键修改点

  • 为CosmosDBContainerPartitionKey添加Paths属性,值为包含分区键路径的列表(例如"/myPartitionKey",对应文档中的myPartitionKey字段)
  • 可选:将预定义的throughputProperties赋值给容器资源信息,启用自动缩放吞吐量

内容的提问来源于stack exchange,提问作者Vaibhav Singla

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 16:45:42