简体   繁体   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

When cloning/copying child records, I use a foreach loop and then create a record with all its attributes.克隆/复制子记录时,我使用 foreach 循环,然后创建一个包含其所有属性的记录。 I wrote similar code in another project and worked fine for me.我在另一个项目中编写了类似的代码,对我来说效果很好。

There are multiple articles/questions based on the same Error.有多个文章/问题基于相同的错误。 Now my issue is how should I create child records with all its attributes.现在我的问题是我应该如何创建具有所有属性的子记录。

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

Another strange issue另一个奇怪的问题

An entry with the same key already exists已存在具有相同键的条目

Code:代码:

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

Even if I create a new object and copy parentEntity just like below I get this error.即使我创建一个新对象并复制parentEntity就像下面一样,我parentEntity收到此错误。

Entity costcalEntity = new Entity();

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

So I end up creating a record with primary name and once the record is created, I update the same record with old record attributes.所以我最终创建了一个带有主名称的记录,一旦创建了记录,我就会用旧的记录属性更新相同的记录。

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);
}

and this works fine.这很好用。

In both cases you have the same issue, with it's root cause being the Id stored in the attribute collection of the entity.在这两种情况下,您都有相同的问题,其根本原因是存储在实体属性集合中的 Id。 If you look at the early bound generation, you can access the Id by the entity.Id property, as well as the attribute collection as shown in the definition for the id in the primary 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");
    }
}

So when you are retrieving an existing entity, both the Property Id, which you have handled, as well as the attribute collection, which you haven't handled, have been populated by the CRM SDK.因此,当您检索现有实体时,您已处理的 Property Id 以及您尚未处理的属性集合都已由 CRM SDK 填充。 So in order to be able to duplicate it, you'll need to clear the id in both places.因此,为了能够复制它,您需要清除两个地方的 id。

Here is how I solved it这是我解决它的方法

 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}");

                    }

So the Trick, issue was rather I was trying to assign packingList directly to newpackingList .所以技巧,问题是我试图将packingList直接分配给newpackingList This caused to assign packingList metadata attributes as well such.这也导致分配packingList元数据属性。 This was not acceptable with crm这对 crm 来说是不可接受的

But rather I should add it's attribute.但我应该添加它的属性。 This worked and created all child records.这有效并创建了所有子记录。

Same worked for parent record as well同样适用于父记录

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);

If your question is "How do I duplicate an Entity retrieved from CRM?", your answer can be simplified.如果您的问题是“如何复制从 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.

相关问题 Microsoft Dynamics CRM - 错误消息:实体ID必须与属性包中设置的值相同 - Microsoft Dynamics CRM - Error message: Entity Id must be the same as the value set in property bag MS Dynamics 365 CRM 在线 - 转储实体 - MS Dynamics 365 CRM online - dump entity 使用QueryExpression选择一个实体? C#Dynamics CRM Online 2015年 - Select an Entity using QueryExpression? C# Dynamics CRM Online 2015 c#-动态CRM在线插件-使用字段值填充相关实体的属性 - c# - dynamics crm online plugin - use field value to populate attribute of related entity Dynamics CRM 2016 c#使用尚不存在的实体的ID - Dynamics CRM 2016 c# use id of not yet existing entity Dynamics CRM &amp; .NET (C#) 为所有行设置字段值 - Dynamics CRM & .NET (C#) Set field value for all rows Microsoft Dynamics 365 CRM 身份验证的示例代码,然后重定向到 C# 控制台应用程序 - sample code for Microsoft Dynamics 365 CRM authentication and then redirect to C# console application 使用 C# 代码从 DYNAMICS 365 crm 检索/获取记录 - Retrieve/fetch records from DYNAMICS 365 crm using C# code 无法在 Dynamics CRM 365 online 中创建系统用户 - Unable to Create a systemuser in Dynamics CRM 365 online 在Dynamics CRM 2015 Online(C#)中访问扩展数据 - Access extension data in Dynamics CRM 2015 Online (C#)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM