1

我正在编写一个将实体导入 CRM 的简单应用程序。在此导入期间,我需要将导入的实体(自定义实体)关联到另一个(也是自定义)实体。

新对象没有问题,但是当我尝试更新时,我需要删除有关导入实体的所有关联并根据导入的数据重新创建它们。

我怎样才能做到这一点?

我正在考虑获取所有关联实体,然后为每个实体调用 disassociate,但我在尝试获取这些关联实体时遇到了困难。

我应该如何处理这个?

4

1 回答 1

0

假设您有学生实体,并且您想将学生课程复制到另一个自定义实体,如您所说,名为 CStudent
您可以使用以下代码:

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);
于 2015-04-02T07:04:04.880 回答