使用实体类添加或更新相关记录之间的关联

  发布日期: 2017年1月

适用于: Dynamics 365 (online),Dynamics 365 (on-premises),Dynamics CRM 2016,Dynamics CRM Online

在 Microsoft Dynamics 365(在线或本地) 中,您可以使用 IOrganizationServiceAssociateIOrganizationServiceDisassociate 方法用来创建和删除相关记录之间的关联。

若要创建关联,请首先确定要关联的目标实体的唯一 ID。 然后创建一个要与目标实体关联的实体集合。 接下来,在集合中的实体和目标实体之间定义关系。 最后,将此信息传递到 Associate 方法。 在删除关联时将相同的信息传递到 Disassociate 方法。

以下代码举例说明了如何在相关记录之间创建关联,以及如何解除它们的关联。

// The account ID would typically be passed in as an argument or determined by a query.
// The contact ID would typically be passed in as an argument or determined by a query.
// Associate the accounts to the contact record. 
//Create a collection of the entity ids that will be associated to the contact.
EntityReferenceCollection relatedEntities = new EntityReferenceCollection();
relatedEntities.Add(new EntityReference("account", _account1Id));
relatedEntities.Add(new EntityReference("account", _account2Id));
relatedEntities.Add(new EntityReference("account", _account3Id)); 
// Create an object that defines the relationship between the contact and account.
Relationship relationship = new Relationship("account_primary_contact");
  //Associate the contact with the 3 accounts.
_orgService.Associate("contact", _contactId, relationship, relatedEntities); 
Console.WriteLine("The entities have been associated."); 
//Disassociate the records.
_orgService.Disassociate("contact", _contactId, relationship, relatedEntities); 
Console.WriteLine("The entities have been disassociated.");

另请参阅

在代码中使用晚期绑定实体类
使用实体类进行创建、更新和删除
实体关系行为
示例:创建、检索、更新和删除(晚期绑定)

Microsoft Dynamics 365

© 2017 Microsoft。 保留所有权利。 版权