OrganisationService 更新时自定义插件抛出错误 "Key not present in dictionary"

Custom Plugin throwing error "Key not present in dictionary" when OrganisationService Updates

我有一些更新实体的代码。一切正常,直到它到达这条线

AliasedValue orderproductAlias = (AliasedValue)product.Attributes["orderproduct.salesorderdetailid"];
Entity orderproduct = new Entity("salesorderdetail");
orderproduct.Id = (Guid)orderproductAlias.Value;
orderproduct.Attributes["salesorderdetailid"] = (Guid)orderproductAlias.Value;
orderproduct.Attributes["piv_myFeild"] = myFeildRef;
service.Update(orderproduct); //Code breaks on this line

我得到的错误是

Unhandled Exception: Microsoft.Xrm.Sdk.InvalidPluginExecutionException: System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]: The given key was not present in the dictionary. (Fault Detail is equal to Microsoft.Xrm.Sdk.OrganizationServiceFault).

更新

我确实有一个 PreOrderProductUpdate 方法,但是当在该代码中放置一个断点时它不会停止,所以我只能假设错误在这两点之间。

AliasedValue orderproductAlias = (AliasedValue)product.Attributes["orderproduct.salesorderdetailid"];

//-- Are you sure the above line is not throwing error ?


//-- Do you have the following check in your plugin code ?
if (context.Depth > 1)
  {
   return;
  }

按以下方式更改您的代码,然后重试:

if(product.Attributes.Contains("orderproduct.salesorderdetailid"))
{
 AliasedValue orderproductAlias = (AliasedValue)product.Attributes["orderproduct.salesorderdetailid"];
 Entity orderproduct = new Entity("salesorderdetail");
 orderproduct.Id = ((EntityReference)orderproductAlias).Id; //It should be parsed as EntityReference not Guid
 orderproduct["piv_myFeild"] = myFeildRef;
 service.Update(orderproduct); //Code breaks on this line
}