简体   繁体   中英

Removing an entity reference in Dynamics API

I'm new to Microsoft Dynamics web API and I need to remove/null an entity reference (a field holding a GUID which is an ID of a record in another entity) in a batch update call. This batch update call could contain any number of records and each record could contain any number of entity reference fields that need to be nulled. To set an entity reference, I'm able to do the following:

"gnh_address1_countryid@odata.bind":"/gnh_countries(c58790c2-ed83-e511-80f7-1458d043a570)"

However, to remove this, setting the value to null (or an empty GUID string of 0s) does not seem to work. I've come across this page

https://msdn.microsoft.com/en-us/library/mt607875.aspx#bkmk_Removeareferencetoanentity

which tells me that I need to do a delete call for a given field that I want to remove. Is this really the only way of doing it? That seems to me to be a lot of calls to what should be a fairly simple thing to do. From what I understand, if I had 3 records that I wanted to update, each of which had 3 entity reference fields that I wanted to remove/delete/nullify, that would be 1 call for the batch update, and then 9 subsequent delete calls. If I had 1000 records to update, this would be an update and then 3000 delete calls. Again, is this really the only way of doing it? Is there no way to remove them as part of the batch update?

Unfortunately, this is the only available way , even today.

Microsoft released v9 web api version, after multiple v8.x versions, but still deleting the reference property/single valued navigation property is the possible way. Setting null to Lookup field (Foreign Key) while update is impossible.

使用Web API操作将set-null与其他字段更新组合在一起

Assign the value Guid. empty, and create a plugin, listening on validate update, and then set the value to null in the plugin worked for me. it is a more simple approach than building an action. I needed this approach because i am updating several fields in same patch from web-api code.

If you are using Kipon.Solid.Plugin framework for buidling plugins, the plugin method will be as simple as below:

public void OnValidateUpdate(Entities.kp_responsibilitymatrix target)
{
      if (target.kp_Solution != null && target.kp_Solution.Id == Guid.Empty)
      {
          target.kp_Solution = null;
      }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM