0

我有一个约会实体,它与联系实体有关联。我试图在没有明确提供contact_id 的情况下从此约会实体中删除所有关联。这是我的代码片段,其中包含创建约会实体,然后与现有联系人关联。

Entity activity = new Entity("appointment");
activity["scheduledstart"] = DateTime.Now;
activity["scheduledend"] = DateTime.Now.AddMinutes(30);
activity["subject"] = "Test Meeting";
activity["description"] = "Test Description";
activity["owneridname"] = "test_user";
activity["location"] = "Dallas";

EntityCollection attendees = new EntityCollection();
Entity attendee1 = new Entity("activityparty");
attendee1["addressused"] = "test.test@acmegroup.com";
attendees.Entities.Add(attendee1);
activity["requiredattendees"] = attendees;

Guid id = _service.Create(activity);
Console.WriteLine("id: " + id);

AssociateRequest associateRequest = new AssociateRequest();
associateRequest.Relationship = new Relationship("new_appointment_contact");
associateRequest.Target = new Microsoft.Xrm.Sdk.EntityReference("appointment", id);

EntityReferenceCollection referenceCollection = new EntityReferenceCollection();
Microsoft.Xrm.Sdk.EntityReference entityReference = new Microsoft.Xrm.Sdk.EntityReference("contact", new Guid("e6e71e53-b44b-e211-a81e-0050568b36bf"));
referenceCollection.Add(entityReference);
associateRequest.RelatedEntities = referenceCollection;

// Execute the request.
_service.Execute(associateRequest);

我知道使用 DisassociateRequest 删除此关联,但我不想明确提供contact_id。我只需要像.Clear()这样可以删除所有联系人关联的东西。

DisassociateRequest disassociateRequest = new DisassociateRequest();
disassociateRequest.Relationship = new Relationship("new_appointment_contact");
disassociateRequest.Target = new Microsoft.Xrm.Sdk.EntityReference("appointment", id);

EntityReferenceCollection referenceCollection2 = new EntityReferenceCollection();
Microsoft.Xrm.Sdk.EntityReference entityReference2 = new Microsoft.Xrm.Sdk.EntityReference("contact", new Guid("e6e71e53-b44b-e211-a81e-0050568b36bf"));
referenceCollection2.Add(entityReference2);
disassociateRequest.RelatedEntities = referenceCollection2;

// Execute the request.
_service.Execute(disassociateRequest);
4

1 回答 1

2

我使用 GetRelatedEntities 方法将所有相关实体拉入集合,然后使用此集合解除所有相关实体的关联:

DisassociateRequest disassociateRequest = new DisassociateRequest();
disassociateRequest.Relationship = new Relationship("new_appointment_contact");
disassociateRequest.Target = new Microsoft.Xrm.Sdk.EntityReference("appointment", id);

EntityReferenceCollection referenceCollection2 = new EntityReferenceCollection();
Entity existingAppointment = _service.Retrieve("appointment", id, new ColumnSet(true));
foreach (Entity item in existingAppointment.GetRelatedEntities(orgContext, "new_appointment_contact"))
{
    Guid contactId = new Guid(item["contactid"].ToString());
    Microsoft.Xrm.Sdk.EntityReference entityReference2 = new Microsoft.Xrm.Sdk.EntityReference("contact", contactId);
    referenceCollection2.Add(entityReference2);
}
disassociateRequest.RelatedEntities = referenceCollection2;

_service.Execute(disassociateRequest);
于 2013-03-01T20:23:16.740 回答