本文包括使用后期绑定和早期绑定编程样式的示例。 详细信息:
基本创建
以下示例演示如何使用后期绑定和早期绑定编程样式创建表行。
每个示例都使用一个 svc 变量,该变量表示实现了 IOrganizationService 接口中方法的类的实例。 有关支持此接口的类的信息,请参阅 IOrganizationService 接口。
注释
每个表都有一个唯一标识符主键列,你可以在创建行时指定该列。 在大多数情况下,应允许系统为你设置此设置,因为系统生成的值经过优化,以实现最佳性能。
Dataverse 将主键数据存储在遥测中,以帮助维护服务。 如果指定自定义的主键值,请不要在这些值中使用敏感信息。
使用弹性表,可以创建包含重复主键值和不同 partitionid 值的记录。 但是,此模式与模型驱动或画布Power Apps不兼容。
了解有关在弹性表中设置主键值的内容
以下示例演示如何使用 Entity 类 通过 IOrganizationService.Create 方法创建帐户。
//Use Entity class with entity logical name
var account = new Entity("account");
// set attribute values
// string primary name
account["name"] = "Contoso";
// Boolean (Two option)
account["creditonhold"] = false;
// DateTime
account["lastonholdtime"] = new DateTime(2017, 1, 1);
// Double
account["address1_latitude"] = 47.642311;
account["address1_longitude"] = -122.136841;
// Int
account["numberofemployees"] = 500;
// Money
account["revenue"] = new Money(new decimal(5000000.00));
// Picklist (Option set)
account["accountcategorycode"] = new OptionSetValue(1); //Preferred customer
//Create the account
Guid accountid = svc.Create(account);
使用 CreateRequest 类
您可以通过将实体实例设置为 CreateRequest.Target property,然后使用 IOrganizationService.Execute 方法获取 CreateResponse 实例,来将后期绑定 Entity 类或早期绑定的生成实体类与 CreateRequest 结合使用,而不是使用 IOrganizationService.Create 方法。 创建的帐户表行的 ID 位于 CreateResponse.id 属性值中。
var request = new CreateRequest() { Target = account };
var response = (CreateResponse)svc.Execute(request);
Guid accountid = response.id;
何时使用 CreateRequest 类
如果要传递可选参数,则必须使用 CreateRequest 类 。 有两种情况可能需要特殊参数。
- 当您想要应用重复检测规则时。 详细信息:使用 SDK 为 .NET 检测重复数据
- 当您创建表示解决方案组件的行(例如 WebResource)并且希望将其与特定解决方案相关联时。 在这种情况下,将使用
SolutionUniqueName参数包含 Solution.UniqueName 的值。 更多信息:通过适用于 .NET 的 SDK 使用消息
使用 CreateMultipleRequest 类
需要创建同一类型的多个记录时, CreateMultipleRequest 类 是性能最高的方法。 详细信息: 大容量操作消息
在一个操作中创建相关实体
对于标准表,创建新表行时,还可以在同一操作中创建相关行。 这称为深层插入。
以下后期绑定和早期绑定示例使用 contact account_primary_contact 一对多关系(其中 account primarycontactid 查找是 ReferencingAttribute)创建一个客户和与该客户关联的联系人。
这些示例还使用 account Account_Tasks 一对多关系(其中 task regardingobjectid 查找是 ReferencingAttribute)创建三个相关的任务行。
在后期绑定样式中,必须显式将一个或多个相关实体(行)添加到 EntityCollection,然后使用 Relationship 类,通过关系的 SchemaName 指定该关系,之后才能将它们添加到 Entity.RelatedEntities 属性。
// Use Entity class with entity logical name
var account = new Entity("account");
// Set attribute values
// string primary name
account["name"] = "Sample Account";
// Create Primary contact
var primaryContact = new Entity("contact");
primaryContact["firstname"] = "John";
primaryContact["lastname"] = "Smith";
// Add the contact to an EntityCollection
EntityCollection primaryContactCollection = new EntityCollection();
primaryContactCollection.Entities.Add(primaryContact);
// Set the value to the relationship
account.RelatedEntities[new Relationship("account_primary_contact")] = primaryContactCollection;
// Add related tasks to create
var taskList = new List<Entity>() {
new Entity("task") { ["subject"] = "Task 1" },
new Entity("task") { ["subject"] = "Task 2" },
new Entity("task") { ["subject"] = "Task 3" }
};
// Add the tasks to an EntityCollection
EntityCollection tasks = new EntityCollection(taskList);
// Set the value to the relationship
account.RelatedEntities[new Relationship("Account_Tasks")] = tasks;
// Create the account
Guid accountid = svc.Create(account);
在创建时关联表行
在创建新行时,可以采用与更新现有行时相同的方式将任何新行与现有行相关联。 必须使用EntityReference来设置查找列(属性)的值。
对于早期和后期绑定样式,此查找分配是相同的。
//Use Entity class with entity logical name
var account = new Entity("account");
// set attribute values
//string primary name
account["name"] = "Sample Account";
Guid primarycontactid = new Guid("e6fa5509-2582-e811-a95e-000d3af40ae7");
account["primarycontactid"] = new EntityReference("contact", primarycontactid);
//Create the account
Guid accountid = svc.Create(account);
使用备用键
如果您不知道该表行的 ID,并且满足以下条件:
- 已为表配置备用键
- 您知道关键价值
可以使用 keyName 和 keyValue 参数来使用备用 EntityReference 构造函数。
account["primarycontactid"] = new EntityReference("contact", "sample_username", "john.smith123");
注释
备用键通常仅用于数据集成方案
详细信息:
检查重复记录
详细信息:使用 SDK 为 .NET 检测重复数据
从主表行设置默认值
当用户在应用程序中创建新行时,通常会在另一行的上下文中创建这些行。 例如,可以在帐户记录中创建新的联系人记录。 发生这种情况时,帐户中的某些列值将复制到联系人窗体中。 这会加快创建新相关行的速度,因为新行设置了一些默认值,以便编辑要创建的行的用户无需输入这些值。 在保存之前,他们可以根据需要更改值。
以这种方式创建新行时复制的值由应用于Microsoft Dataverse环境的配置控制,因此在环境之间可能会有所不同。
详细信息:
作为开发人员,可以使用 InitializeFromRequest 类 生成已设置默认值的实体实例。
以下代码创建与现有帐户关联的新联系人。 该联系人与指定客户相关联,并且某些属性值(如 Telephone1)以及客户和联系人之间共享的各种地址值会默认设置。
//The account that the contact will be associated with:
var parentAccount = new EntityReference("account", new Guid("a976763a-ba1c-e811-a954-000d3af451d6"));
// Initialize a new contact entity with default values from the account.
var request = new InitializeFromRequest()
{
EntityMoniker = parentAccount,
TargetEntityName = "contact"
};
var response = (InitializeFromResponse)svc.Execute(request);
//Get the Entity from the response
Entity contact = response.Entity;
// Set values that are not from the account
contact["firstname"] = "Joe";
contact["lastname"] = "Smith";
// Create the contact
Guid contactid = svc.Create(contact);
使用 Upsert
创建表行的另一种方法是使用 UpsertRequest 类。 当没有现有行包含随请求一起传递的行中包含的唯一标识符时,upsert 将创建一个新行。
详细信息: 使用 Upsert
在存储分区中创建文档
如果要创建包含非关系数据的大量弹性表行,则可以在存储分区中创建这些行,以加快对这些行的访问速度。 详细信息: 分区和水平缩放
另请参阅
使用 SDK for .NET 检索表行
使用 SDK for .NET 更新和删除表行
使用 SDK for .NET 关联和取消关联表行