發行︰ 2017年1月
適用於: Dynamics 365 (online)、Dynamics 365 (on-premises)、Dynamics CRM 2016、Dynamics CRM Online
在 Microsoft Dynamics 365 (線上和內部部署) 中,您可以使用 IOrganizationService.Associate 和 IOrganizationService.Disassociate 方法來建立和移除相關記錄之間的關聯。
若要建立關聯,請先確定目標實體的唯一識別碼將會關聯。 您接著建立的實體集合,使與目標實體產生關聯。 接著,您定義集合中實體與目標實體間的關聯。 最後,您傳遞此資訊至 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. 著作權所有,並保留一切權利。 著作權