Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dynamics 365(CRM)9.1版联机C#代码错误:实体Id必须与属性包中设置的值相同_C#_Entity Framework_Entity Framework 6_Dynamics Crm_Dynamics Crm Online - Fatal编程技术网

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

Dynamics 365(CRM)9.1版联机C#代码错误:实体Id必须与属性包中设置的值相同,c#,entity-framework,entity-framework-6,dynamics-crm,dynamics-crm-online,C#,Entity Framework,Entity Framework 6,Dynamics Crm,Dynamics Crm Online,在克隆/复制子记录时,我使用foreach循环,然后创建一个包含其所有属性的记录。我在另一个项目中编写了类似的代码,效果很好 存在多篇基于同一错误的文章/问题。现在我的问题是如何创建具有所有属性的子记录 foreach (var packingList in oPEntityCollection.Entities) { packingList.Attributes.Remove("statuscode");

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

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

foreach (var packingList in oPEntityCollection.Entities)
{                                        
    packingList.Attributes.Remove("statuscode");
    packingList.Attributes.Remove("statecode"); 
    packingList.Id=Guid.Empty; 
    orgService.Create(packingList);
}
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);
}
另一个奇怪的问题

具有相同密钥的条目已存在

代码:

即使我创建一个新对象并复制
parentEntity
,就像下面一样,我也会遇到这个错误

Entity costcalEntity = new Entity();

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

因此,我最终创建了一个具有主名称的记录,一旦创建了该记录,我将使用旧记录属性更新该记录

foreach (var packingList in oPEntityCollection.Entities)
{                                        
    packingList.Attributes.Remove("statuscode");
    packingList.Attributes.Remove("statecode"); 
    packingList.Id=Guid.Empty; 
    orgService.Create(packingList);
}
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);
}
Entity costcalEntity=new Entity();
costcalEntity.LogicalName=父实体.LogicalName;
costcalEntity[“name”]=parentQuotationEntity.GetAttributeValue(“name”);
costcalEntity.Id=Guid.Empty;
Guid newGuid=orgService.Create(成本实体);
if(newGuid!=Guid.Empty)
{
costcalEntity=父实体;
costcalEntity.Id=newGuid;
orgService.Update(成本日历);
}

这很好。

在这两种情况下,您都有相同的问题,其根本原因是存储在实体属性集合中的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");
    }
}
公共系统。可为空的帐户ID
{
[System.Diagnostics.DebuggerNonUserCode()]
得到
{
返回此.GetAttributeValue(“accountid”);
}
[System.Diagnostics.DebuggerNonUserCode()]
设置
{
此项。OnPropertyChange(“AccountId”);
此.SetAttributeValue(“accountid”,值);
if(value.HasValue)
{
base.Id=value.value;
}
其他的
{
base.Id=System.Guid.Empty;
}
此.OnPropertyChanged(“AccountId”);
}
}

因此,在检索现有实体时,CRM SDK已填充已处理的属性Id和未处理的属性集合。因此,为了能够复制它,您需要清除两个位置的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(实体);

我在两个地方都清除了身份证。如前所述,我为另一个项目编写了几乎相同的代码,并且工作得很好。但我发现,与其将检索到的实体直接分配给新的实体对象,不如只添加它的属性,然后是,不要忘记删除主Id,因为它将由CRM创建。在您的示例中,您没有显示自己在属性集合中清除Id。是的,我在第一段代码中没有提到。刚刚更新了我的代码。这是我复制记录的前四行代码,如前所述,相同的代码与另一个env一起工作,但对于我正在编写的代码给我的那个env,具有相同密钥的条目已经存在,因此我尝试了一些其他方法,例如仅使用主名称创建实体,然后使用父记录对象更新此记录。长话短说这在当前场景中不起作用,但是代码在运行另一个实例时没有问题。@AnkUser我想我对“复制记录的前四行代码”感到困惑。您是在谈论您的答案吗?因为在这个问题中,没有什么地方可以从属性集合中删除Id,只有状态/状态。很抱歉,我的意思是,当我开始编写插件时,上面四行是我的出发点,我希望这能解决问题,但事实并非如此。正如我的回答和问题中所述,仅使用相同的实体对象并删除其Id返回一个具有相同密钥的条目