CRM 2015 SDK OrganizationService.Associate: 如果1 in collections errors, others are still associate or all rollback?

CRM 2015 SDK OrganizationService.Associate: If 1 in collections errors, do others still associate or all rollback?

在 CRM 2015 SDK 中,当您使用 OrgService.Associate(包装在 try/catch 中)将 EntityReferenceCollection 关联到实体时,如果任何一个 EntityReferences 失败,整个 ERC 是否会失败观点?或者说ERC中有10个ER,第7个失败了,是不是前6个成功了,后面4个不处理?还是 ERC 中的所有 10 个都得到处理和关联,只有失败的 #7 除外?

这有意义吗?

Associate 消息在单个事务中处理,因此如果任何相关实体失败,整个事务都会回滚。

您可以通过执行以下代码轻松测试它:

EntityReferenceCollection relatedEntities = new EntityReferenceCollection();
relatedEntities.Add(new EntityReference("account", new Guid("A88C66DD-0B29-E811-8126-5065F38AEA61"))); // Existing account #1
relatedEntities.Add(new EntityReference("account", Guid.NewGuid())); // Non-existing account
relatedEntities.Add(new EntityReference("account", new Guid("BA647CEA-0B29-E811-8126-5065F38AEA61"))); // Existing account #3

service.Associate("contact", new Guid("0A3B1F8B-77F0-E711-811F-5065F38B06F1"), new Relationship("account_primary_contact"), relatedEntities);

这将以抛出 FaultException 告终:

System.ServiceModel.FaultException`1 occurred HResult=0x80131501
Message=account With Id = 50a65bfc-1ed3-49de-b910-adef17febe3b Does Not Exist Source=mscorlib StackTrace: at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) at Microsoft.Xrm.Sdk.IOrganizationService.Associate(String entityName, Guid entityId, Relationship relationship, EntityReferenceCollection relatedEntities)

并且您将能够检查是否没有为帐户 #1 和 #3 创建任何关联。