我正在使用 C# OData 4 客户端,如下所述:
我有一个产品类别,它有 ID、名称、价格和类别。我想做这样的事情:
var product = new ProductService.Models.Product {
Id = 2,
Price = 4
};
container.AttachTo("Products", product);
container.UpdateObject(product);
这样我就可以只更新价格属性并忽略所有其余的属性。我可以看到这不起作用,因为在创建 Product 对象时名称和类别被创建为 null,因此它们将在结果请求中作为 null 发送。
有没有办法在不先检索我想要更新的对象的情况下执行此操作? (我猜我需要走 HttpClient 路线)。
最佳答案
一种解决方法是直接使用 HttpClient:
HttpRequestMessage request = new HttpRequestMessage(new HttpMethod("PATCH"), requestUri);
request.Content = new StringContent(@"{{""@odata.type"":""#ProductService.Models.Product"",""Price"":3000}}");
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
HttpResponseMessage response = new HttpClient.SendAsync(request).Result;
关于c# - 客户端 OData 补丁无需先检索对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25120132/