在天蓝色函数中,我尝试更新动态中的实体:
public async Task UpdateEmployeeAsync(JObject employee)
{
var content = new StringContent(employee.ToString(), Encoding.UTF8, "application/json");
var response = await _httpClient.PostAsync($"/data/EmployeesV2", content);
response.EnsureSuccessStatusCode();
}
我也尝试过类似的事情:
var response = await _httpClient.PostAsync($"/data/EmployeesV2/{employee["PersonnelNumber"]}", content);
每次我收到
internal server error
回复。
我在 Postman 中尝试了同样的操作,并提出了各种类型的请求:
PATCH
https://xxxxxxxxxxx.dynamics.com/data/EmployeesV2(PersonnelNumber='100059')
BODY
{
"FirstName": "xxx",
"LastName": "xxxx",
"PrimaryContactEmail": "xxxxxxxx.com",
"PersonnelNumber": "100059",
"EmploymentLegalEntityId": "0093",
"EmploymentStartDate": "2021-09-27T00:00:00-05:00",
"TitleId": " Chief Executive Officer"
}
我得到的回复是:
{
"Message": "No HTTP resource was found that matches the request URI 'https://xxxxxxxxxxxx.dynamics.com/data/EmployeesV2(PersonnelNumber='100059')'. No route data was found for this request."
}
知道如何在 C# 中修补 EmployeeV2 实体吗?
使用以下请求:
PATCH
https://xxxxxxxxxxx.dynamics.com/data/EmployeesV2(PersonnelNumber='100059',EmploymentLegalEntityId='0093',EmploymentId='100059')
BODY
{
"FirstName": "xxx",
"LastName": "xxxx",
"PrimaryContactEmail": "xxxxxxxx.com",
"PersonnelNumber": "100059",
"EmploymentLegalEntityId": "0093",
"EmploymentStartDate": "2021-09-27T00:00:00-05:00",
"TitleId": " Chief Executive Officer"
}
注意:我必须猜测 URL 中
的值。就我而言,它与EmploymentId
相同。请务必检查您的数据。PersonnelNumber
要使用 PATCH 请求更新现有记录,您需要使用 规范 URL。这是实体集与括号中的关键属性一起使用的 URL。另请参阅 D365FO Microsoft 文档中的OData 服务。
问题就变成了,所使用的实体集的关键属性是什么?如果您熟悉整体数据模型并且可以访问D365FO开发环境,您可以查看设计器中的实体定义。
如果您没有该信息,您可以使用元数据文档来获取实体集的关键属性。元数据文档可通过以下 URL 获取(可能需要一段时间才能获得响应 - 对于版本 10.0.39,该文档大小为 39 MB):
https://xxxxxxxxxxx.dynamics.com/data/$metadata
在元数据文档中,您可以搜索实体集名称
EmployeesV2
并查找 EntitySet
元素。这将有一个 EntityType
属性,该属性将为您提供实体类型的名称。然后,您可以搜索具有该名称的 EntityType
元素(不带 Microsoft.Dynamics.DataEntities.
前缀)以获取关键属性。
<EntitySet Name="EmployeesV2" EntityType="Microsoft.Dynamics.DataEntities.EmployeeV2">
<EntityType Name="EmployeeV2">
<Key>
<PropertyRef Name="PersonnelNumber" />
<PropertyRef Name="EmploymentLegalEntityId" />
<PropertyRef Name="EmploymentId" />
</Key>