In Microsoft Dynamics 365 (online & on-premises), 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.

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 365. You can modify, create, and delete objects in the organization service context, and Microsoft Dynamics 365 tracks the changes that you made to these objects. When the OrganizationServiceContext.SaveChanges method is called, Microsoft Dynamics 365 generates and executes commands that perform the equivalent insert, update, or delete statements against data in Microsoft Dynamics 365.

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.

When you want to insert data into Microsoft Dynamics 365 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 365.

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 365 data fail.

Microsoft Dynamics 365 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 365 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.

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.

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 365.

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.