为什么我的CRM插件中所有参考属性都为空? [英] Why are all reference properties null in my CRM plugin?

查看:88
本文介绍了为什么我的CRM插件中所有参考属性都为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用早期绑定在联系人实体上编写PostUpdate插件.
不幸的是,所有应该表示1:x关系的属性都为空.
代码很简单:
* CRMcontext是通过CrmSvcUtil.exe生成的文件,
*服务是LocalPluginContext的IOrganizationService:

I'm writing a PostUpdate Plugin on the contact entity using early binding.
Unfortunately, all properties which should represent 1:x relations are null.
The code is pretty simple:
* CRMcontext is the generated file via CrmSvcUtil.exe,
* service is the IOrganizationService from LocalPluginContext:

using ( var serviceContext = new CRMcontext(service) )
{
  // This works fine
  var contact = serviceContext.CreateQuery<Contact>().First(c => c.Id == context.PrimaryEntityId);

  // why is currency null after this line?! (and yes, it's set in the entity)
  var currency = contact.transactioncurrency_contact;
}

我遵循了此示例(最后一个代码片段): http://msdn .microsoft.com/en-us/library/gg695791.aspx

I followed this example (the last code snippet): http://msdn.microsoft.com/en-us/library/gg695791.aspx

感谢您的帮助!

/// <summary>
/// N:1 transactioncurrency_contact
/// </summary>
[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("transactioncurrencyid")]
[Microsoft.Xrm.Sdk.RelationshipSchemaNameAttribute("transactioncurrency_contact")]
public TransactionCurrency transactioncurrency_contact
{
    get
    {
        return this.GetRelatedEntity<TransactionCurrency>("transactioncurrency_contact", null);
    }
    set
    {
        this.OnPropertyChanging("transactioncurrency_contact");
        this.SetRelatedEntity<TransactionCurrency>("transactioncurrency_contact", null, value);
        this.OnPropertyChanged("transactioncurrency_contact");
    }
}

推荐答案

CRM不会自动加载相关的实体属性.您需要致电 LoadProperty 延迟加载的每个属性.

CRM doesn't load the related entity properties automatically. You'll need to call LoadProperty on each property that is lazily loaded.

LameCoder是不正确的,LINQ to CRM不会生成Fetch Xml,但是会生成QueryExpressions,这就是为什么它仅限于QueryExpressions具有的任何功能的原因.

And LameCoder is incorrect, LINQ to CRM doesn't generate Fetch Xml, but QueryExpressions, which is why it is limited to whatever capability QueryExpressions possess.

GetRelatedEntity方法的定义如下:

The GetRelatedEntity method is defined as so:

protected virtual IEnumerable<TEntity> GetRelatedEntities<TEntity>(string relationshipSchemaName, EntityRole? primaryEntityRole) where TEntity : Entity
{
  if (string.IsNullOrWhiteSpace(relationshipSchemaName))
    throw new ArgumentNullException("relationshipSchemaName");
  Relationship key = new Relationship(relationshipSchemaName)
  {
    PrimaryEntityRole = primaryEntityRole
  };
  if (!this.RelatedEntities.Contains(key))
    return (IEnumerable<TEntity>) null;
  else
    return Enumerable.Cast<TEntity>((IEnumerable) this.RelatedEntities[key].Entities);
}

如果您的早期绑定实体是从Entity继承的,那么它唯一要做的就是访问它自己的内部RelatedEntities集合.它不执行任何操作来访问服务器以加载相关属性.

If your early bound entity inherits from Entity, then only thing it is doing is accessing it's own internal RelatedEntities collection. It is doing nothing to access the server to load the related property.

如果您使用 CodeGeneration.CodeCustomization 来生成早期绑定实体,它应该如您列出的那样工作,因为它将继承CrmEntity,因为它将覆盖您的关系,因为它覆盖了GetRelatedEntity方法,该方法使用上下文为您获取它.

If you use the CodeGeneration.CodeCustomization to generate the early bound entities it should work as you have listed, because it'll inherit from CrmEntity, which will load the relationships for you since it overrides the GetRelatedEntity method uses the context to fetch it for you.

这篇关于为什么我的CRM插件中所有参考属性都为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆