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

Use the Early Bound Entity Classes for Create, Update, and Delete

[Applies to: Microsoft Dynamics CRM 2011]

In Microsoft Dynamics CRM 2011 you can use the entity data model and the early-bound entity classes, created by the code generation tool (CrmSvcUtil), to work with business data. You can use these early-bound classes with or without the organization service context, but a best practice is to use the generated organization service context class. The context attempts to manage relationships efficiently, but handwritten code can typically be even more efficient.

noteNote
Updates to records are made in a specific order. First, primary entities are processed, and then related entities are processed. If a change is made by the primary entity for a lookup or related entity attribute, and then a related entity updates the same attribute, the related entity value is retained. In general, a lookup attribute value and its equivalent in the RelatedEntities (or navigation properties) for the same relationship should not be used at the same time.

In This Topic

Use the Organization Service Context Class

The organization service context class that the code generation tool creates and that inherits from OrganizationServiceContext is used to track data changes. The context tracks objects that are instances of entity types that represent data in Microsoft Dynamics CRM. You can modify, create, and delete objects in the organization service context, and Microsoft Dynamics CRM tracks the changes that you made to these objects. When the OrganizationServiceContext.SaveChanges method is called, Microsoft Dynamics CRM generates and executes commands that perform the equivalent insert, update, or delete statements against data in Microsoft Dynamics CRM.

When working with early-bound entity classes, you use the entity name and attribute schema name to specify an entity or attribute to work with. Attribute schema names are defined in EntityMetadata.SchemaName and AttributeMetadata.SchemaName, or you can use the class and property names shown in the code-generated file.The following sample shows how to assign a value to the email attribute of a new contact instance.

Contact contact = new Contact();
contact.EMailAddress1 = “sonny@contoso.com”;

For a complete code sample that shows how to use early-bound entity classes to perform basic database actions, see Sample: Create, Retrieve, Update, and Delete Records (Early Bound).

Create a New Entity Record Using the Early-bound Entity Classes and the Organization Service Context

When you want to insert data into Microsoft Dynamics CRM by using the entity data model, you must create an instance of an entity type and add the object to an organization service context. The organization service context must be tracking the object before it can save the object to Microsoft Dynamics CRM.

When creating a new entity record, you add the object to the organization service context by using the OrganizationServiceContext.AddObject method.

The following sample shows how to instantiate and save a new contact record by using the entity data model. It also demonstrates how tp access a custom attribute.

OrganizationServiceContext orgContext =new OrganizationServiceContext(_serviceProxy);
Contact contact = new Contact()
   {
   FirstName = "Charles",
   LastName = "Brown",
   Address1_Line1 = "123 Main St.",
   Address1_City = "Des Moines",
   Address1_StateOrProvince = "IA",
   Address1_PostalCode = "21254",
   new_twittername = "Chuck",
   Telephone1 = "123-234-5678"
   }; 
orgContext.AddObject(contact);
orgContext.SaveChanges();

After you add an object to the context and before the OrganizationServiceContext.SaveChanges method is called, the context generates an ID for the new object. An exception that contains the SaveChangesResults is thrown from the SaveChanges method if any updates to the Microsoft Dynamics CRM data fail.

Update an Entity Record Using Early-Bound Entity Classes and the Organization Service Context

Microsoft Dynamics CRM tracks changes to objects that are attached to the organization service context. To modify an existing entity record, you must first add the object to the context. To add an object to the context, you must first retrieve the entity record from Microsoft Dynamics CRM and then add the object to the context by using the OrganizationServiceContext.Attach method. Once the object is being tracked by the context, you can update the record by setting the entity’s attributes.

The following sample shows how to update an account attribute by using early bound classes.

Account.EMailAddress1 = “Contoso-WebMaster@contoso.com”;

The following sample shows how to delete an attribute value.

Account.EMailAddress1 = null;

There are two partial methods named OnPropertyChanging and OnPropertyChanged for each entity. These methods are called in the property setter. You can extend these methods by using partial classes to insert custom business logic.

Delete an Entity Record Using Early-Bound Entity Classes and the Organization Service Context

To delete an entity record, the organization service context must be tracking the object. Once the object is on the context, you can use the OrganizationServiceContext.DeleteObject method to mark the object on the context for deletion. Note that the entity record in Microsoft Dynamics CRM is not deleted until the OrganizationServiceContext.SaveChanges method is called.

Use Early-Bound Entity Classes without a Context Object

You can use the early-bound entity classes without creating an organization service context object if you do not want to create the context object. The OrganizationServiceProxy class includes a OrganizationServiceProxy. Create method that can be used to save entity record changes to Microsoft Dynamics CRM.

The following sample shows how to use an early-bound entity class without creating an organization service context object. The OrganizationServiceProxy. Create method returns the GUID id assigned to the newly created entity record.

Contact contact = new Contact()
   {
   FirstName = "Charles",
   LastName = "Brown",
   Address1_Line1 = "123 Main St.",
   Address1_City = "Des Moines",
   Address1_StateOrProvince = "IA",
   Address1_PostalCode = "21254",
   Telephone1 = "123-234-5678"
   }; 
_contactId = _serviceProxy.Create(contact);

To update an entity record in Microsoft Dynamics CRM, you retrieve the data to be updated, make the necessary changes, and then use the OrganizationServiceProxy. Update method to commit those changes to Microsoft Dynamics CRM. To retrieve entity records, you use the OrganizationServiceProxy. Retrieve method for single-object retrieval or the OrganizationServiceProxy. RetrieveMultiple method for retrieving multiple objects. To delete an entity record, you use the OrganizationServiceProxy. Delete method.

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.