繁体   English   中英

Dynamics 365 (CRM) 9.1 版在线 C# 代码错误:实体 ID 必须与属性包中设置的值相同

[英]Dynamics 365 (CRM) Version 9.1 online C# code Error: Entity Id must be the same as the value set in property bag

克隆/复制子记录时,我使用 foreach 循环,然后创建一个包含其所有属性的记录。 我在另一个项目中编写了类似的代码,对我来说效果很好。

有多个文章/问题基于相同的错误。 现在我的问题是我应该如何创建具有所有属性的子记录。

foreach (var packingList in oPEntityCollection.Entities)
{                                        
    packingList.Attributes.Remove("statuscode");
    packingList.Attributes.Remove("statecode"); 
    packingList.Id=Guid.Empty; 
    orgService.Create(packingList);
}

另一个奇怪的问题

已存在具有相同键的条目

代码:

Entity parentEntity = orgService.Retrieve(context.PrimaryEntityName, context.PrimaryEntityId, new ColumnSet(true));    
parentEntity.Id = Guid.empty;
orgService.Create(parentEntity);

即使我创建一个新对象并复制parentEntity就像下面一样,我parentEntity收到此错误。

Entity costcalEntity = new Entity();

costcalEntity = parentEntity;
costcalEntity.Id = Guid.Empty;
orgService.Create(costcalEntity);

所以我最终创建了一个带有主名称的记录,一旦创建了记录,我就会用旧的记录属性更新相同的记录。

Entity costcalEntity = new Entity();
costcalEntity.LogicalName = parentEntity.LogicalName;
costcalEntity["name"] = parentQuotationEntity.GetAttributeValue<string>("name");
costcalEntity.Id = Guid.Empty;
Guid newGuid = orgService.Create(costcalEntity);
if (newGuid != Guid.Empty)
{
    costcalEntity = parentEntity;
    costcalEntity.Id = newGuid;
    orgService.Update(costcalEntity);
}

这很好用。

在这两种情况下,您都有相同的问题,其根本原因是存储在实体属性集合中的 Id。 如果查看早期绑定代,则可以通过 entity.Id 属性访问 Id,以及主 id 中的 id 定义中显示的属性集合:

public System.Nullable<System.Guid> AccountId
{
    [System.Diagnostics.DebuggerNonUserCode()]
    get
    {
        return this.GetAttributeValue<System.Nullable<System.Guid>>("accountid");
    }
    [System.Diagnostics.DebuggerNonUserCode()]
    set
    {
        this.OnPropertyChanging("AccountId");
        this.SetAttributeValue("accountid", value);
        if (value.HasValue)
        {
            base.Id = value.Value;
        }
        else
        {
            base.Id = System.Guid.Empty;
        }
        this.OnPropertyChanged("AccountId");
    }
}

因此,当您检索现有实体时,您已处理的 Property Id 以及您尚未处理的属性集合都已由 CRM SDK 填充。 因此,为了能够复制它,您需要清除两个地方的 id。

这是我解决它的方法

 foreach (Entity packingList in oPEntityCollection.Entities)
                {
                    Entity newpackingList = new Entity()
                    {
                        LogicalName = packingList.LogicalName,
                    };
                    newpackingList.Attributes.AddRange(packingList.Attributes);                        
                    newpackingList.Attributes.Remove("primaryGuid");                       
                    Guid newOpGuid = orgService.Create(newpackingList);    
                    tracingService.Trace($"OP record created sucessfully with guid {newOpGuid}");

                    }

所以技巧,问题是我试图将packingList直接分配给newpackingList 这也导致分配packingList元数据属性。 这对 crm 来说是不可接受的

但我应该添加它的属性。 这有效并创建了所有子记录。

同样适用于父记录

Entity parentEntity = orgService.Retrieve(context.PrimaryEntityName, context.PrimaryEntityId, new ColumnSet(true)); 

   Entity newParentEntity = new Entity()
                        {
                            LogicalName = parentEntity.LogicalName,
                        };
newParentEntity.Attributes.AddRange(parentEntity.Attributes); 
 newParentEntity.Attributes.Remove("primaryGuid");
orgService.Create(newParentEntity);

如果您的问题是“如何复制从 CRM 检索到的实体?”,您的答案可以简化。

var entity = orgService.Retrieve(context.PrimaryEntityName, context.PrimaryEntityId, new ColumnSet(true)); 
entity.Id = Guid.Empty;
entity.Attributes.Remove("primaryGuid");
orgService.Create(entity);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM