访问实体关系 (Dynamics CRM 2015)

 

发布日期: 2016年11月

适用于: Dynamics CRM 2015

Microsoft Dynamics 365 开发人员扩展也为检索相关实体提供了帮助程序方法。OrganizationServiceContext 的标准行为要求首先显式加载实体关系成员,然后才能访问相关实体,如本例所示。

var connection = CrmConnection.Parse("Url=http://crm.contoso.com/xrmContoso; ProxyTypesAssembly=Xrm;");

using (var service = new OrganizationService(connection))
using (var context = new OrganizationServiceContext(service))
{
var contact = context.CreateQuery<Contact>().First(c => c.FirstName == "Bob");

var currencyEmpty = contact.GetRelatedEntity<Entity>("transactioncurrency_contact");
var currencyEmptyStatic = contact.transactioncurrency_contact;

// currency is always null until after calling LoadProperty

Console.WriteLine(currencyEmpty == null);
Console.WriteLine(currencyEmptyStatic == null);

context.LoadProperty(contact, "transactioncurrency_contact");

// read related entity dynamically
var currency = contact.GetRelatedEntity<Entity>("transactioncurrency_contact");
Console.WriteLine(currency.GetAttributeValue<string>("currencyname"));

// read related entity statically
var currencyStatic = contact.transactioncurrency_contact;
Console.WriteLine(currencyStatic.CurrencyName);
}

对于那些更愿意使用静态类型成员而非基于字符串的关系和属性名称的用户而言,可使用一组采用关系参数作为静态表达式的帮助程序方法。 表达式采用 lambda 表达式的形式,只带一个实体参数。 帮助程序重载对于包括以下方法在内的所有 OrganizationServiceContext 关系方法都存在:
LoadProperty
GetRelatedEntity
GetRelatedEntities
AttachLink
DetachLink
AddLink
DeleteLink
AddRelatedObject

以下示例演示了如何使用 LoadProperty 帮助程序方法。

using Microsoft.Xrm.Client;

using (var service = new OrganizationService(connection))
using (var context = new OrganizationServiceContext(service))
{
var contact = context.CreateQuery<Contact>().First(c => c.FirstName == "Bob");
context.LoadProperty(contact, c => c.transactioncurrency_contact);
var currency = contact.transactioncurrency_contact;
Console.WriteLine(currency.CurrencyName);
}

另一组帮助程序隐式调用 LoadProperty 方法并采用 OrganizationServiceContext 引用作为第一个参数。 这适用于此处所示的 GetRelatedEntityGetRelatedEntities 方法。

using (var service = new OrganizationService(connection))
using (var context = new OrganizationServiceContext(service))
{
// use helper method that calls LoadProperty implicitly
var contact = context.CreateQuery<Contact>().First(c => c.FirstName == "Bob");
var currency = contact.GetRelatedEntity(context, "transactioncurrency_contact");
Console.WriteLine(currency.GetAttributeValue<string>("currencyname"));
}

using (var service = new OrganizationService(connection))
using (var context = new OrganizationServiceContext(service))
{
// use helper method that calls LoadProperty implicitly and maintains static typing
var contact = context.CreateQuery<Contact>().First(c => c.FirstName == "Bob");
var currency = contact.GetRelatedEntity(context, c => c.transactioncurrency_contact);
Console.WriteLine(currency.CurrencyName);
}

通过使用 CrmOrganizationServiceContext 或生成的 XrmServiceContext,您具有用于访问关系的最简洁代码。 此上下文允许通过内部跟踪上下文引用,并隐式调用 LoadProperty 来自动延迟相关实体加载,如此处所示。

using (var service = new OrganizationService(connection))
using (var context = new CrmOrganizationServiceContext(service))
{
// this context manages the context reference internally
var contact = context.CreateQuery<Contact>().First(c => c.FirstName == "Bob");
var currency = contact.transactioncurrency_contact;
Console.WriteLine(currency.CurrencyName);
}

另请参阅

开发人员扩展上下文对象模型 (Dynamics CRM 2015)
使用配置文件配置上下文 (Dynamics CRM 2015)
将实体附加到上下文 (Dynamics CRM 2015)
背景增强功能

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