![](/img/trans.png)
[英]Dynamics 365 (CRM) Version 9.1 online C# code Error: Entity Id must be the same as the value set in property bag
[英]Microsoft Dynamics CRM - Error message: Entity Id must be the same as the value set in property bag
您好StackOverflow社區,
我只是嘗試復制“聯系人”的記錄 - 來自插件的實體或自定義工作流活動。 相關守則是
QueryExpression qe = new QueryExpression("contact")
{
ColumnSet = new ColumnSet("firstname", "lastname")
};
EntityCollection entityCollection = _organizationService.RetrieveMultiple(qe);
foreach (Entity entity in entityCollection.Entities)
{
entity.Id = Guid.NewGuid();
if (!entity.Attributes.Contains("firstname"))
{
entity.Attributes.Add("firstname", "");
}
entity["firstname"] = (entity.GetAttributeValue<string>("firstname") ?? "") + "(Copy)";
_organizationService.Create(entity);
}
不幸的是,我總是收到錯誤消息
“實體ID必須與屬性包中設置的值相同”。
如果我遺漏了這條線
Entity.Id = Guid.NewGuid();
然后我收到了錯誤
“無法插入重復的密鑰。”
我還嘗試了各種其他方法來構建一個新的Guid,包括
byte [] bytes = new byte[16];
random.NextBytes(bytes);
entity.Id = new Guid(bytes);
要么
entity.Id = Guid.Empty;
結果也是
“實體ID必須與屬性包中設置的值相同”。
另一方面,我有一個桌面應用程序,我借助本文https://msdn.microsoft.com/en-us/library/jj602970.aspx連接到我的Microsoft CRM 2016 Office 365系統並可以復制記錄行。
任何幫助是極大的贊賞。
QueryExpression
總是返回id,作為實際的Entity.Id
,它的屬性名稱為contactid
。 這就是你得到錯誤的原因。 您可以刪除此屬性,但最佳做法是始終創建新的C#Contact對象,而不是更新從CRM中檢索的對象。 此外,您不希望設置自己的GUID,因為CRM使用的序列GUID更適合索引和排序。
QueryExpression qe = new QueryExpression("contact")
{
ColumnSet = new ColumnSet("firstname", "lastname")
};
foreach (Entity entity in _organizationService.RetrieveMultiple(qe).Entities)
{
var copy = new Entity();
copy["firstname'] = (entity.GetAttributeValue<string>("firstname") ?? "") + "(Copy)";
_organizationService.Create(copy);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.