使用OData V4客户端更新动态实体

时间:2017-09-25 23:28:57

标签: c# odata dynamics-crm

尝试使用OData v4客户端更新实体属性,以某种方式出现此错误:

xxx_accountid是导航属性,但我没有更新此属性,其类型可以为空。

{
  "error": {
    "code": "",
    "message": "Property _xxx_accountid_value cannot be updated to null. The reference property can only be deleted.",
    "innererror": {
      "message": "Property _xxx_accountid_value cannot be updated to null. The reference property can only be deleted.",
      "type": "Microsoft.Crm.CrmHttpException",
      "stacktrace": "   at Microsoft.Crm.Extensibility.OData.CrmODataUtilities.ValidatePatchInputProperties(EdmEntityObject entityDelta)\r\n   at Microsoft.Crm.Extensibility.OData.EntityController.PatchEntity(String entityName, String key, EdmEntityObject entityDelta)\r\n   at lambda_method(Closure , Object , Object[] )\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n "
    }
  }
}

解决。事实证明_xxx_accountid是由CRM本身生成的导航属性。我在T4模板中删除了这个属性,但它确实有效。

1 个答案:

答案 0 :(得分:2)

正如错误中所述

  

只能删除引用属性

在更新调用中无法将Lookup设置为null(这很奇怪,但确实如此)

您可以通过调用对象的引用属性的DELETE来清理引用

/api/data/v8.2/contacts([ID])/xxx_accountid/$ref
// instead of contacts set changed entity

Details about remove reference request