[英]Microsoft Dynamics CRM - Error message: Entity Id must be the same as the value set in property bag
Hi StackOverflow community, 您好StackOverflow社区,
I just try to copy records of the "contact"- Entity from a Plugin or a custom workflow activity. 我只是尝试复制“联系人”的记录 - 来自插件的实体或自定义工作流活动。 The relevant Code is
相关守则是
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);
}
Unfortunately, I'm getting always the error message 不幸的是,我总是收到错误消息
"Entity Id must be the same as the value set in property bag". “实体ID必须与属性包中设置的值相同”。
If I leave out the line 如果我遗漏了这条线
Entity.Id = Guid.NewGuid();
then I'm getting the error 然后我收到了错误
"Cannot insert duplicate key." “无法插入重复的密钥。”
I also tried various other ways to construct a new Guid, including 我还尝试了各种其他方法来构建一个新的Guid,包括
byte [] bytes = new byte[16];
random.NextBytes(bytes);
entity.Id = new Guid(bytes);
or 要么
entity.Id = Guid.Empty;
resulting also in 结果也是
"Entity Id must be the same as the value set in property bag". “实体ID必须与属性包中设置的值相同”。
On the other hand, I have a desktop app where I connect to my Microsoft CRM 2016 Office 365 System with the help of this article https://msdn.microsoft.com/en-us/library/jj602970.aspx and can copy records all right. 另一方面,我有一个桌面应用程序,我借助本文https://msdn.microsoft.com/en-us/library/jj602970.aspx连接到我的Microsoft CRM 2016 Office 365系统并可以复制记录行。
Any help is greatly appreciated. 任何帮助是极大的赞赏。
The QueryExpression
always returns the id, both as the actual Entity.Id
, and it's property name contactid
. QueryExpression
总是返回id,作为实际的Entity.Id
,它的属性名称为contactid
。 That is why you are getting the error. 这就是你得到错误的原因。 You could just remove this attribute, but best practice is to always create a new C# Contact object, rather than updating one being retrieved from CRM.
您可以删除此属性,但最佳做法是始终创建新的C#Contact对象,而不是更新从CRM中检索的对象。 Also, you don't want to be setting your own GUID, since CRM uses sequential GUIDS that are better optimized for indexing and ordering.
此外,您不希望设置自己的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.