Skip to content

Add or Remove N to N Relationship via C#

There is no status or status reason for N:N relationship’s record, only add (associate) or remove (disassociate).

Below is sample code for Associate:

var hobbyEnt = new Entity(Hobby.EntityName, hobbyId);

var entRefCol = new EntityReferenceCollection();
entRefCol.Add(hobbyEnt.ToEntityReference());

var relationship = new Relationship(app_hobby_contact.EntityName);
crmService.Associate(Contact.EntityName, contactId, relationship, entRefCol);

Below is sample code for Disassociate:

var hobbyEnt = new Entity(Hobby.EntityName, hobbyId);

var entRefCol = new EntityReferenceCollection();
entRefCol.Add(hobbyEnt.ToEntityReference());

var relationship = new Relationship(app_hobby_contact.EntityName);
crmService.Disassociate(Contact.EntityName, contactId, relationship, entRefCol);

Basically, we are adding the hobby entity into the EntityReferenceCollection and associate/disassociate to the contact by trigger respective request.

You may refer official SDK for more info: https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/org-service/entity-operations-associate-disassociate

Leave a Reply

Your email address will not be published. Required fields are marked *