Aracılığıyla paylaş


Use the early-bound entity classes for create, update, and delete

 

Applies To: Dynamics CRM 2015

In Microsoft Dynamics CRM 2015 and Microsoft Dynamics CRM Online 2015 Update, 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.

Note

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 OrganizationServiceContext class

Create a new entity record using the early-bound entity classes and the OrganizationServiceContext class

Update an entity record using early-bound entity classes and the OrganizationServiceContext class

Delete an entity record using early-bound entity classes and the OrganizationServiceContext class

Use early-bound entity classes without a Context object

Use the OrganizationServiceContext 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 OrganizationServiceContext class

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 OrganizationServiceContext class

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 OrganizationServiceContext class

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

Use the early bound entity classes in code
Use the early bound entity classes to add or update associations between related records
Create early bound entity classes with the code generation tool (CrmSvcUtil.exe)
Sample: Create, retrieve, update, and delete records (early bound)

© 2016 Microsoft. All rights reserved. Copyright