【发布时间】:2019-10-31 22:15:21
【问题描述】:
我正在尝试更新我的数据库中的特定产品。我从正文传递产品值并替换它。这里我的分区键是 catlog id.blow 我给出了所有我的代码示例请告诉我我做错了什么?
Json 正文:
{
"id": "9fc41e97-c32e-45e6-853f-ae7993cbc773",
"VendorName": "string",
"Industy": {
"Id": "40f5a6f5-2947-469c-8d29-6aa84de982cb",
"IdustryId": 0,
"IdustryName": "string",
"Category": {
"id": "bf16c9a1-52c6-4e88-b24a-082591718ece",
"Name": "string",
"Description": "string",
"Subcategory": {
"id": "c60a191a-f5b8-4cff-8926-e4b364f8aba2",
"Name": "string",
"Description": "string",
"Product": {
"Id": "97c27069-4a16-44c3-bdf1-627c8ea339fa",
"Name": "string",
"CurrentQuantity": 0,
"Tag": "string",
"Unit": "string",
"Price": 0,
"hasMethodOfPreparation": true,
"Addons": {
"id": "1b7afbc0-36df-4a97-951e-4f0fef0c683f",
"Description": "string",
"Price": 0
},
"MethodOfPreparation": {
"id": "9e1581cf-8401-44c0-ac8b-15e972276178",
"Description": "string",
"Price": 0
}
}
}
}
}
控制器代码:
[HttpPut("UpdateProductByIdAsync/{id}", Name = "UpdateProductById")]
public async Task<IActionResult> UpdateProductByIdAsync(
[FromRoute] Guid id,
[FromBody] Product order)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id == null
|| id == Guid.Empty)
{
return BadRequest("Id required.");
}
order.Id = id;
var result =
await _catalogRepository.UpdateProductByIdAsync(
id,
order);
return Ok(result);
}
存储库
public async Task<Catalog> UpdateProductByIdAsync(
Guid id,
Product order)
{
var requestOptions =
new RequestOptions
{
PartitionKey = new Microsoft.Azure.Documents.PartitionKey(id.ToString())
};
var catalogDocument = await _cosmosClient.ReplaceDocumentAsync(
UriFactory.CreateDocumentCollectionUri(
_azureCosmosDbOptions.Value.DatabaseId, "catalog"), order, requestOptions);
return
(Catalog)((dynamic)catalogDocument.Resource);
}
}
错误:
<pre class="rawExceptionStackTrace">Microsoft.Azure.Documents.BadRequestException: ResourceType Document is unexpected.&#xD;&#xA;ActivityId: 62f61852-d2a7-466a-9765-80db1d02de32, Windows/10.0.17134 documentdb-netcore-sdk/2.4.0&#xD;&#xA; at Microsoft.Azure.Documents.PathsHelper.GeneratePathForNameBased(ResourceType resourceType, String resourceFullName, Boolean isFeed, Boolean notRequireValidation)&#xD;&#xA; at Microsoft.Azure.Documents.GatewayStoreModel.GetEntityUri(DocumentServiceRequest entity)&#xD;&#xA; at Microsoft.Azure.Documents.GatewayStoreModel.ProcessMessageAsync(DocumentServiceRequest request, CancellationToken cancellationToken)&#xD;&#xA; at Microsoft.Azure.Documents.Client.DocumentClient.ProcessRequestAsync(DocumentServiceRequest request, IDocumentClientRetryPolicy retryPolicyInstance, CancellationToken cancellationToken)&#xD;&#xA; at Microsoft.Azure.Documents.Client.DocumentClient.ReplaceDocumentPrivateAsync(String documentLink, Document document, RequestOptions options, IDocumentClientRetryPolicy retryPolicyInstance, CancellationToken cancellationToken)&#xD;&#xA; at Microsoft.Azure.Documents.BackoffRetryUtility`1.ExecuteRetryAsync(Func`1 callbackMethod, Func`3 callShouldRetry, Func`1 inBackoffAlternateCallbackMethod, TimeSpan minBackoffForInBackoffCallback, CancellationToken cancellationToken, Action`1 preRetryCallback)&#xD;&#xA; at Microsoft.Azure.Documents.ShouldRetryResult.ThrowIfDoneTrying(ExceptionDispatchInfo capturedException)&#xD;&#xA; at Microsoft.Azure.Documents.BackoffRetryUtility`1.ExecuteRetryAsync(Func`1 callbackMethod, Func`3 callShouldRetry, Func`1 inBackoffAlternateCallbackMethod, TimeSpan minBackoffForInBackoffCallback, CancellationToken cancellationToken, Action`1 preRetryCallback)&#xD;&#xA; at Microsoft.Azure.Documents.Client.DocumentClient.ReplaceDocumentInlineAsync(String documentLink, Object document, RequestOptions options, CancellationToken cancellationToken)&#xD;&#xA; at CatalogAPI.Services.CatalogRepository.UpdateProductByIdAsync(Guid id, Product order) in D:\UCDP\UCDP\UCDP\src\Service\Catalog\CatalogAPI\Services\CatalogRepository.cs:line 322&#xD;&#xA; at CatalogAPI.Controllers.CatalogController.UpdateProductByIdAsync(Guid id, Product order) in D:\UCDP\UCDP\UCDP\src\Service\Catalog\CatalogAPI\Controllers\CatalogController.cs:line 158&#xD;&#xA; at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)&#xD;&#xA; at System.Threading.Tasks.ValueTask`1.get_Result()&#xD;&#xA; at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()&#xD;&#xA; at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()&#xD;&#xA; at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)&#xD;&#xA; at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted)&#xD;&#xA; at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()&#xD;&#xA; at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()&#xD;&#xA; at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)&#xD;&#xA; at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted)&#xD;&#xA; at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()&#xD;&#xA; at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()&#xD;&#xA; at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)&#xD;&#xA; at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)&#xD;&#xA; at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)&#xD;&#xA; at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)&#xD;&#xA; at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)&#xD;&#xA; at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)</pre>
请帮我完成这项任务。我尝试了 upsertDocumentAsync 但创建了一个新的文档文件而不是更新现有的文件。
【问题讨论】:
标签: azure asp.net-web-api azure-cosmosdb