我正在尝试从 azure 函数的 Cosmos DB 数据库容器中删除一个项目。但我收到此错误必须为此操作提供 PartitionKey 值。我也对此进行了调查,但没有从那里得到任何运气。
下面是我的代码 -
public class Item{
public Item(){}
public string id {get; set;}
}
public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
DocumentClient client = new DocumentClient(new Uri("https://***.documents.azure.com:443/"), Environment.GetEnvironmentVariable("key"));
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var updated = JsonConvert.DeserializeObject<Item>(requestBody);
var option = new FeedOptions { EnableCrossPartitionQuery = true };
var collectionUri = UriFactory.CreateDocumentCollectionUri("Test_DB", "Test_Table");
var document = client.CreateDocumentQuery(collectionUri, option).Where(t => t.Id == updated.id)
.AsEnumerable().FirstOrDefault();
log.LogInformation("Total Incentive : " + document.GetPropertyValue<string>("id"));
if (document == null)
{
return new NotFoundResult();
}
log.LogInformation("Total Incentive : " + document.SelfLink);
log.LogInformation("Total Incentive : " + document.Id);
await client.DeleteDocumentAsync(document.SelfLink);
//await client.ReplaceDocumentAsync(document);
return new OkObjectResult("Deleted");
}
谷歌搜索后,我意识到我需要提供一个分区键值作为DeleteDocumentAsync()中的第二个参数,如下所示。但我正在修复如何为该函数提供该分区键值。
await client.DeleteDocumentAsync(document.SelfLink,
new Requestoptions
{
PartitionKey = new Microsoft.Azure.Documents.PartitionKey("33333")
}
)
数据库名称: Test_DB
表名: Test_Table
分区键: /testCategory
我试图删除的 Test_Table 的一个示例元素是:
{
"id": "3f614t4e-q85f-4357-8393-a0b3542db4e1",
"Email": "testuser@test.com",
"Name": "Test User",
"_rid": "XNMBANxVc8oIAAAAAAAAAA==",
"_self": "dbs/XNMOXA==/colls/TNMNANyVc9o=/docs/XNMBANxVc8oIZZAAAAAAPP==/",
"_etag": "\"450031d4-0000-0300-0000-60cb36470000\"",
"_attachments": "attachments/",
"_ts": 1623930439
}
谁能帮助我如何将该分区键放入 azure 函数代码中?或者如何调整数据库的表?
提前致谢。