Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Apps - Power Apps Pro Dev & ISV
Answered

Remove Dataverse Relationship in C# Plugin

(0) ShareShare
ReportReport
Posted on by 559

Hello Community!

 

I have the following usecase: A Trainee get's deleted from a Training. That training has Extra Costs which are related to the Trainee. On deleting the Trainee from the Training the Trainee should get unrelated from his Extra Costs (which doesn't happen now). Is it possible in C# syntax to unrelate Extra Cost ID from TraineeID?

 

This is how the relations are build:

  • Training 1:M Extra Costs
  • Trainee M:M Extra Costs

Best Regards,

Anthony

  • Verified answer
    AnthonyD Profile Picture
    559 on at
    Re: Remove Dataverse Relationship in C# Plugin

    Somehow it was easier then i expected. Here is where i found the solution:

    https://docs.microsoft.com/en-us/powerapps/developer/data-platform/org-service/entity-operations-associate-disassociate

     

    And this is the code i used (it compares 2 collections and adds the matching  ounces to a collection. These are the ounces to unrelate)

    double traineeExtraKostKost=0;
     EntityCollection TraineeExtraKostsCollection = new EntityCollection();
     foreach (var extrakost in opleidingExtraKostResult.Entities)
     {
     foreach (var traineeExtraKost in cursistExtraKosts.Entities)
     {
     if (traineeExtraKost.Id==extrakost.Id)
     {
     traineeExtraKostKost += Convert.ToDouble(traineeExtraKost.Attributes.FirstOrDefault(q => q.Key == "cref8_prijs").Value);
     TraineeExtraKostsCollection.Entities.Add(extrakost);
     }
     }
     
     }
     var extraKostReference = new EntityReferenceCollection();
     TraineeExtraKostsCollection.Entities.ToList().ForEach(x =>
     {
     extraKostReference.Add(x.ToEntityReference());
     });
     var relationship = new Relationship("cref8_ExtraKost_cref8_Cursist_cref8_Cursi");
     service.Disassociate("cref8_cursist", relatedEntity.Id, relationship, extraKostReference);

     

    Best Regards,

    Anthony

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Leaderboard > Power Apps - Power Apps Pro Dev & ISV

#1
WarrenBelz Profile Picture

WarrenBelz 109 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 76 Super User 2025 Season 1

#3
mmbr1606 Profile Picture

mmbr1606 71 Super User 2025 Season 1

Overall leaderboard