C# Cosmos DB 补丁 - 将元素插入子集合
C# Cosmos DB Patch - insert element into child collection
正在尝试将新项目添加到父对象的子集合中。例如,在下面的代码中,如何向 Items 集合添加一个新的 Item?
{
"id": "SalesOrder2",
"ponumber": "PO15428132599",
"OrderDate": "2005-07-01T00:00:00",
"AccountNumber": "Account2",
"Items": [
{
"Id": 1,
"OrderQty": 3,
"ProductCode": "A-123",
"ProductName": "Product 1",
"CurrencySymbol": "$",
"CurrencyCode": "USD",
"UnitPrice": 17.1,
"LineTotal": 5.7
},
{
"Id": 2,
"OrderQty": 2,
"ProductCode": "A-456",
"ProductName": "Product 2",
"CurrencySymbol": "$",
"CurrencyCode": "USD",
"UnitPrice": 10,
"LineTotal": 20
}
],
"_rid": "BsMkAMc43s4CAAAAAAAAAA==",
"_self": "dbs/BsMkAA==/colls/BsMkAMc43s4=/docs/BsMkAMc43s4CAAAAAAAAAA==/",
"_etag": "\"00000000-0000-0000-e136-0dbec04601d7\"",
"_attachments": "attachments/",
"_ts": 1637760030
}
做这样的事情:
public async Task AddProduct(Item item)
{
var response = await CosmosDbContainer.PatchItemAsync<Product>(
id: "a1dc8286-bb5e-4ac0-beb4-f9f1dfc79711",
partitionKey:new PartitionKey("a1dc8286-bb5e-4ac0-beb4-f9f1dfc79711"),
patchOperations: new[] { PatchOperation.Add("/Items", item) });
}
returns 出现以下错误:
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[TestApp.Models.Item]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly
传递项目列表有效,但它会替换所有子项,例如:
public async Task AddProduct(Item item)
{
var itemList = new List<Item>();
itemList.Add(item);
var response = await CosmosDbContainer.PatchItemAsync<Product>(
id: "a1dc8286-bb5e-4ac0-beb4-f9f1dfc79711",
partitionKey:new PartitionKey("a1dc8286-bb5e-4ac0-beb4-f9f1dfc79711"),
patchOperations: new[] { PatchOperation.Add("/Items", ItemList) });
}
如有任何建议,我们将不胜感激。提前致谢!
您试过通过索引吗?您需要通过传递要更新的对象的特定索引来使用添加操作,
patchOperations: new[] { PatchOperation.Add("/Items/1", item) });
或者用“-”追加
patchOperations: new[] { PatchOperation.Add("/Items/-", item) });
正在尝试将新项目添加到父对象的子集合中。例如,在下面的代码中,如何向 Items 集合添加一个新的 Item?
{
"id": "SalesOrder2",
"ponumber": "PO15428132599",
"OrderDate": "2005-07-01T00:00:00",
"AccountNumber": "Account2",
"Items": [
{
"Id": 1,
"OrderQty": 3,
"ProductCode": "A-123",
"ProductName": "Product 1",
"CurrencySymbol": "$",
"CurrencyCode": "USD",
"UnitPrice": 17.1,
"LineTotal": 5.7
},
{
"Id": 2,
"OrderQty": 2,
"ProductCode": "A-456",
"ProductName": "Product 2",
"CurrencySymbol": "$",
"CurrencyCode": "USD",
"UnitPrice": 10,
"LineTotal": 20
}
],
"_rid": "BsMkAMc43s4CAAAAAAAAAA==",
"_self": "dbs/BsMkAA==/colls/BsMkAMc43s4=/docs/BsMkAMc43s4CAAAAAAAAAA==/",
"_etag": "\"00000000-0000-0000-e136-0dbec04601d7\"",
"_attachments": "attachments/",
"_ts": 1637760030
}
做这样的事情:
public async Task AddProduct(Item item)
{
var response = await CosmosDbContainer.PatchItemAsync<Product>(
id: "a1dc8286-bb5e-4ac0-beb4-f9f1dfc79711",
partitionKey:new PartitionKey("a1dc8286-bb5e-4ac0-beb4-f9f1dfc79711"),
patchOperations: new[] { PatchOperation.Add("/Items", item) });
}
returns 出现以下错误:
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[TestApp.Models.Item]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly
传递项目列表有效,但它会替换所有子项,例如:
public async Task AddProduct(Item item)
{
var itemList = new List<Item>();
itemList.Add(item);
var response = await CosmosDbContainer.PatchItemAsync<Product>(
id: "a1dc8286-bb5e-4ac0-beb4-f9f1dfc79711",
partitionKey:new PartitionKey("a1dc8286-bb5e-4ac0-beb4-f9f1dfc79711"),
patchOperations: new[] { PatchOperation.Add("/Items", ItemList) });
}
如有任何建议,我们将不胜感激。提前致谢!
您试过通过索引吗?您需要通过传递要更新的对象的特定索引来使用添加操作,
patchOperations: new[] { PatchOperation.Add("/Items/1", item) });
或者用“-”追加
patchOperations: new[] { PatchOperation.Add("/Items/-", item) });