共用方式為


使用實體類別,進行建立、更新和刪除

發行︰ 2017年1月

適用於: Dynamics 365 (online)、Dynamics 365 (on-premises)、Dynamics CRM 2016、Dynamics CRM Online

在 Microsoft Dynamics 365 (線上和內部部署),您可以使用 Entity 類別建立、更新或刪除實體與實體屬性。

使用實體類別建立、更新和刪除

當您使用 Entity 類別並使用晚期繫結,您處理實體和邏輯屬性名稱。 這與使用實體與屬性結構描述名稱的早期繫結不同。 邏輯屬性名稱是全小寫,而結構描述屬性名稱使用駝峰式大小寫。

若要建立新的實體,您要先建立 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. 著作權所有,並保留一切權利。 著作權