使用实体类进行创建、更新和删除

  发布日期: 2017年1月

适用于: Dynamics 365 (online),Dynamics 365 (on-premises),Dynamics CRM 2016,Dynamics CRM Online

在 Microsoft Dynamics 365(在线或本地) 中,您能够使用 Entity 类创建和删除实体机实体属性。

使用实体类创建、更新和删除

在使用 Entity 类和晚期绑定时,您可以使用实体和逻辑属性名称。 这与早期绑定有显著的差别,在后者可以使用实体和属性架构名称。 逻辑属性名称全部小写,而架构属性名称使用 Pascal 大小写格式。

若要创建新实体,首先要创建 Entity 类的新实例并为其传递一个实体名称。 以下代码示例显示如何创建新的客户记录。

// Instantiate an account object.
Entity account = new Entity("account");

// Set the required attributes. For account, only the name is required. 
// See the metadata to determine 
// which attributes must be set for each entity.
account["name"] = "Fourth Coffee";

// Create an account record named Fourth Coffee.
_accountId = _orgService.Create(account);

在本示例中,将会创建类型为“客户”的新实体对象,设置属性,然后调用 IOrganizationService.Create 方法以创建新记录。

若要更新实体,您可以设置要更新的属性的值,然后调用 IOrganizationService.Update 方法。 以下代码示例显示如何更新帐户中的属性。

Entity account = new Entity("account");
// Create a column set to define which attributes should be retrieved.
ColumnSet attributes = new ColumnSet(new string[] { "name", "ownerid" }); 

// Retrieve the account and its name and ownerid attributes.
account = _orgService.Retrieve(account.LogicalName, _accountId, attributes);

// Update the postal code attribute.
account["address1_postalcode"] = "98052";

// Update the account.
_orgService.Update(account);

若要删除某个实体,您可以将重要的属性信息传递到 IOrganizationService.Delete 方法。 以下代码示例显示如何使用 Delete 方法。

_orgService.Delete("account", _accountId);

另请参阅

使用实体类添加或更新相关记录之间的关联
在代码中使用晚期绑定实体类

Microsoft Dynamics 365

© 2017 Microsoft。 保留所有权利。 版权