【问题标题】:Relation between Retieve AND Update in a Dynamic CRM PLUGIN动态 CRM 插件中检索和更新之间的关系
【发布时间】:2014-10-13 01:29:09
【问题描述】:

动态 CRM 插件中的检索和更新之间是否存在关系? 例如,如果我只检索一个字段:

Entity e = (Entity)service.Retrieve("EntityLogicalName", EntityGuid, 
new ColumnSet(new string[] {"entityid"}));

我可以更新实体 e 中尚未检索到的另一个字段吗? 例如:

e.Attributes["AnotherEntityField1] = "test1";
e.Attributes["AnotherEntityField2] = "test2";
service.update(e);

如果不包括要在检索中更新的所有字段,这可能会导致一些隐藏的问题吗?

【问题讨论】:

    标签: dynamics-crm-2011 dynamics-crm-2013


    【解决方案1】:

    假设您只是检索实体的主键entityid,则无需进行检索。

    Entity e = new Entity("EntityLogicalName") { Id = EntityGuid };
    e.Attributes.Add("AnotherEntityField1", "test1");
    e.Attributes.Add("AnotherEntityField2", "test2");
    service.Update(e);
    

    如果您正在执行检索以确认记录存在,您需要尝试/捕获或使用检索倍数,因为如果记录不存在,Retrieve 将引发异常。

    【讨论】:

      【解决方案2】:

      您尝试做的事情是完全可以接受的,不会造成任何问题。由于您通过 Retrieve 操作获得了 Entity 实例,因此将正确设置所需的 LogicalName 和 Id 以进行更新。

      您的代码需要如下所示添加最初未检索到的新属性,否则您将获得KeyNotFoundException,因为Entity 类型只是Dictionary<string,string> 的包装器。

      e.Attributes.Add("AnotherEntityField2","test2");
      

      【讨论】:

        【解决方案3】:

        当您尝试更新实体时,您不需要属性集合中存在字段,但为了避免出现“给定键未出现在字典中”的异常,最好先检查是否属性集合包含您要更新的字段。如果是,只需更新它,否则您必须将其添加到实体的属性集合中。

        if(e.Attributes.Contains("AnotherEntityField1"))
        {
        e.Attributes["AnotherEntityField1"] = "test1";
        }
        else
        {
        e.Attributes.Add("AnotherEntityField1", "test1");
        }
        

        // 现在更新操作

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-01-06
          • 1970-01-01
          • 2013-06-06
          • 1970-01-01
          • 2020-10-05
          • 1970-01-01
          相关资源
          最近更新 更多