Share via


作成、更新、および削除のエンティティ クラスの使用 

公開日: 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);

この例では 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. All rights reserved. 著作権