Cosmos DB中的PartitionKey属性需要始终是一个字符串吗?

时间:2017-11-28 19:35:22

标签: azure-cosmosdb

我正在将下面的类对象序列化并存储到具有分区键路径" / targetId"的Cosmos DB分区集合中。在集合上设置。

public class DataItem
{
    [JsonProperty(PropertyName = "id")]
    public string Id { get; set; }

    [JsonProperty(PropertyName = "name")]
    public string Name { get; set; }

    [JsonProperty(PropertyName = "city")]
    public string City { get; set; }

    /// This is my partition key
    [JsonProperty(PropertyName = "targetId")]
    public long TargetId { get; set; }
}

在上面的类中,当我将TagretId属性设置为long时,我得到以下错误的错误:

Requests originating from scripts cannot reference partition keys other 
than the one for which client request was submitted

但是,当我将TargetId属性更改为" string"时,它可以正常工作。

与Cosmos DB一起使用时,分区键属性是否有任何类型的重新属性?

更新了代码

List<DataItem> items = GetDataItems();  // This comes from UI as JSON, actually
var groupedItems = items.GroupBy(x => x.TargetId);
foreach (var groupItem in groupedItems)
{
    // I even tried like below converting to dynamic JSON array. Still got the same exception
    //string argsJson = JsonConvert.SerializeObject(groupItem.ToArray());
    //var args = new dynamic[] { JsonConvert.DeserializeObject<dynamic[]>(argsJson) };

    RequestOptions requestOptions = new RequestOptions { PartitionKey = new PartitionKey(groupItem.Key.ToString()) };

    var result = await client.ExecuteStoredProcedureAsync<int>(
        UriFactory.CreateStoredProcedureUri(DatabaseId, CollectionId, sprocId),
        requestOptions,
        groupItem.ToList());
}

0 个答案:

没有答案