如何传递PartitionKey来执行StoredProcedure?

时间:2016-04-21 15:27:26

标签: azure-cosmosdb

我试图从.net SDK(v1.6.3)在分区集合(服务器端分区)上执行存储过程。

await client.ExecuteStoredProcedureAsync<string>(UriFactory.CreateStoredProcedureUri("db0", "collection0", "testsproc0"), storedProcedureParams);

我收到错误&#34;必须为此操作提供PartitionKey值&#34;。事实上,当我使用Script Explorer时,我在Azure门户中看到了这个错误。但是,我看不到添加PartitionKey的方法。这是现在API和分区集合的限制,还是我错过了什么?

2 个答案:

答案 0 :(得分:8)

您可以通过使用RequestOptions重载的ExecuteStoredProcedureAsync方法传入分区键。例如,

assignConstants()

答案 1 :(得分:1)

我收到了类似的错误,发现我创建文档集时的Partition Key Path不正确,如下面的行collectionDefinition.PartitionKey.Paths.Add("/LastName")所示。我的ExecuteStoredProcedure RequestOption与我在new RequestOptions { PartitionKey = new PartitionKey("matchingLastNameSelectionHere") }中选择的字段不匹配。希望这会有所帮助。

        private static async Task<DocumentCollection> CreateCollectionAsync(string dbLink, string id)
    {
        DocumentCollection collectionDefinition = new DocumentCollection { Id = id };
        collectionDefinition.IndexingPolicy = new IndexingPolicy(new RangeIndex(DataType.String) { Precision = -1 });
        collectionDefinition.PartitionKey.Paths.Add("/LastName");

        return await _client.CreateDocumentCollectionAsync(
            dbLink,
            collectionDefinition,
            new RequestOptions { OfferThroughput = 400 });
    }

执行代码

await client.ExecuteStoredProcedureAsync<DeviceSignal>(UriFactory.CreateStoredProcedureUri("db", "coll", "SetLatestStateAcrossReadings"), new RequestOptions { PartitionKey = new PartitionKey("matchingLastNameSelectionHere") }, sprocsParams);