Summary of Changes Between Versions

[Applies to: Microsoft Dynamics CRM 2011]

In Microsoft Dynamics CRM 2011, the Microsoft Dynamics CRM 4.0 DynamicEntity class has been replaced by the Entity class. The Entity class provides access through strong types to entities and attributes at compile time because all early-bound classes inherit Entity. These include any custom entities and custom attributes for the organization. You can also use the late binding programming model that you used with Microsoft Dynamics CRM 4.0.

The Entity class contains the schema name for all entities and attributes. In programming terms, this is referred to as strong typing because all entities and attributes can be type checked at compile time.

While the DynamicEntity class had to be used with the Execute method in Microsoft Dynamics CRM 4.0 and the ReturnDynamicEntities property was required, you can now use the Entity class with the Retrieve and RetrieveMultiple methods, in addition to the SaveChanges method in the OrganizationServiceContext class.

Setting, Testing, and Excluding Null Values

In earlier versions if Microsoft Dynamics CRM, you could exclude a value from the dynamic entity property bag by setting the property to null. In Microsoft Dynamics CRM 2011, if a property is set to null, a value of null will actually be sent to the server.

Exclude a Value

The following code shows how to exclude a value from the dynamic entity property bag:

//Instantiate an account record.
Account entity; 
//Calling Remove ensures the value is not in the property bag.
entity.Remove("industrycode"); 

The following is the same sample code from Microsoft Dynamics CRM 4.0:

// Instantiate an account record.
account entity; 
//Set the value to null to exclude the value.
entity.industrycode = null;

Test for a null Value

The following code shows how to test for a null value:

Account entity; //Populate the variable
if (null == entity.IndustryCode) 
{ 
// This attribute is null 
}

// Using late-binding
if (null == entity.GetAttributeValue<OptionSetValue>("industrycode")) 
{ 
// This attribute is null 
}

The following is the same code from Microsoft Dynamics CRM 4.0:

account entity = new account(); 
//Populate account 
account.industrycode = new Picklist() { IsNull = true };

Set a null Value

The following code shows how to set a null value:

Account entity; //Populate the variable
entity.IndustryCode = null;
Entity.AccountId = Guid.Empty;
Entity.AccountNumber = “”;
Entity.Address1_Country = String.Empty;

The following is the same code from Microsoft Dynamics CRM 4.0:

account entity; //Populate the variable
entity.industrycode = new Picklist();
entity.industrycode.IsNull = true;

Setting or Accessing the Record ID (Primary Key)

The base entity class defines an ID property that is the primary key for the entity. You no longer have to look up the name for the primary key attribute, and your code is more consistent between entity types.

The following code shows how to set the Entity.Id property. This is the recommended approach because it allows for compile-time checks.

Entity entity = new Entity(Account.EntityLogicalName);
entity.Id = Guid.NewGuid();

The following code shows how to perform the same task by using a property bag:

Entity entity = new Entity(Account.EntityLogicalName);
entity["accountid"] = Guid.NewGuid();

The following is the same code from Microsoft Dynamics CRM 4.0:

DynamicEntity entity = new DynamicEntity();
entity.Name = EntityName.account.ToString();
entity["accountid"] = new Key(Guid.NewGuid());

Entity and Attribute Type Names

In earlier versions of Microsoft Dynamics CRM, all entities and attributes were named according to the logical name (for example, "account" or "name"). In Microsoft Dynamics CRM 2011, all entities and attributes are named according to their schema names. This means that the casing of the names of entities and attributes has changed.

The following code shows the new casing on entity and attribute names:

Contact newEntity = new Contact();
newEntity.ContactId = Guid.NewGuid();
newEntity.FirstName = "Jay";
newEntity.LastName = "Hamlin";

The following is the same code from Microsoft Dynamics CRM 4.0:

contact newEntity = new contact();
newEntity.contactid = new Key(Guid.NewGuid());
newEntity.firstname = "Jay";
newEntity.lastname = "Hamlin";

Entity Constants

In earlier versions of Microsoft Dynamics CRM had an enumeration called EntityName that represented the entity type code for each entity. For Microsoft Dynamics CRM 2011, these two values have been changed to constants on each entity class. 

The following code shows how to use the type code and entity name constants:

int objectTypeCode = Account.EntityTypeCode;
string entityName = Account.EntityLogicalName;

The following is the same code from Microsoft Dynamics CRM 4.0.

int objectTypeCode = (int)EntityName.account;
string entityName = EntityName.account.ToString();

See Also

Concepts

Types in the Microsoft Dynamics CRM SDK

Other Resources

Upgrade Your Code from Microsoft Dynamics CRM 4.0 to Microsoft Dynamics CRM 2011

Microsoft Dynamics CRM 2011
Send comments about this topic to Microsoft.
© 2013 Microsoft Corporation. All rights reserved.