Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Use the Early Bound Entity Classes to Add or Update Associations Between Related Records

[Applies to: Microsoft Dynamics CRM 2011]

In Microsoft Dynamics CRM 2011 and Microsoft Dynamics CRM Online, you can create an association by using early binding in several ways. To create a one-to-many relationship, you can use the AddLink method in the OrganizationServiceContext class. To create a many-to-many relationship, you can use the Associate method in the IOrganizationService class to create an association. You can also create the association by updating the foreign key of the target entity to match the primary key of the new source entity.

To remove an association, you can use the DeleteLink method in the OrganizationServiceContext class or the Disassociate method. You can also set the foreign key to null.

For a complete sample showing how to add and remove associations, see Sample: Associate Records (Early Bound).

Use the AddLink Method

You can use the AddLink method to create associations. You must call the SaveChanges method before the server is updated with the new link information.

The following code example shows how to create an association between a contact and an account:

Relationship relationship = new Relationship("account_primary_contact");
context.AddLink(contact, relationship, account);
context.SaveChanges();


Use the Associate Method

You use the Associate method to create both one-to-many and many-to-many associations. The following code example shows how to create a one-to-many association between an account and a contact:

Relationship relationship2 = new Relationship("account_primary_contact");
EntityReferenceCollection relatedEntities = new EntityReferenceCollection();
relatedEntities.Add(new EntityReference(Account.EntityLogicalName, firstaccount.Id));
_serviceProxy.Associate(Contact.EntityLogicalName, firstcontact.Id, relationship2, relatedEntities);


See Also

Microsoft Dynamics CRM 2011
Send comments about this topic to Microsoft.
© 2013 Microsoft Corporation. All rights reserved.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.