【问题标题】:Crm 2011 - retrieve associated [N:N] entities (C#)Crm 2011 - 检索关联的 [N:N] 实体 (C#)
【发布时间】:2012-05-23 13:29:18
【问题描述】:

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

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

我该怎么做?

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

我应该如何处理这个问题?

【问题讨论】:

标签: c# crm microsoft-dynamics


【解决方案1】:

假设您有学生实体,并且您想将学生课程复制到另一个自定义实体,如您所说,名为 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多