我正在编写一个将实体导入CRM的简单应用程序。在这个导入中,我需要将导入的实体(自定义实体)与另一个(也是自定义的)实体相关联。
新对象没有问题,但是当我尝试更新时,我需要删除有关导入实体的所有关联,并根据导入的数据重新创建它们。
我该怎么做?
我正在考虑获取所有关联实体,然后为每个实体调用取消关联,但是我试图获取这些关联实体时遇到困难。
我该如何处理?
答案 0 :(得分:0)
假设您有学生实体并且您希望将学生课程复制到另一个自定义实体,如您所说的名为学生 您可以使用以下代码:
var scs = Context.new_Student_CourcesSet.Where(x => x.new_courceid == Cource.Id).ToList<new_Student_Cources>();
var removedsc = Context.new_new_CStudent_CourcesSet.Where(x => x.new_cstudentid == CStudent.Id).ToList<new_CStudent_Cources>();
EntityReferenceCollection relatedEntities = new EntityReferenceCollection();
EntityReferenceCollection removedrelatedEntities = new EntityReferenceCollection();
Relationship relationship = new Relationship(new_CStudent_Cources.EntityLogicalName);
if (removedsc != null)
{
foreach (new_CStudent_Cources c in removedsc )
{
RemovedrelatedEntities.Add(new EntityReference(new_Cources.EntityLogicalName, (Guid)ar.new_courcesid));
}
Service.Disassociate(CStudent.LogicalName, CStudnetid, relationship, RemovedrelatedEntities);
}
foreach (new_Student_Cources d in scs)
{
relatedEntities.Add(new EntityReference(new_Cources.EntityLogicalName, (Guid)d.new_courceid));
}
Service.Associate(CStudent.LogicalName, CStudentid, relationship, relatedEntities);