简体   繁体   English

CRM 2011-检索关联的[N:N]个实体(C#)

[英]Crm 2011 - retrieve associated [N:N] entities (C#)

I'm writing a simple application that imports entities to CRM. 我正在编写一个将实体导入CRM的简单应用程序。 Durring this import I need to associate imported entities (custom entity) to another (also custom) entities. 在此导入期间,我需要将导入的实体(自定义实体)与另一个(也是自定义)实体相关联。

There's no problem with new objects, but when I try to update, I need to delete all associations regarding imported entity and recreated them based on imported data. 新对象没有问题,但是当我尝试更新时,我需要删除有关导入实体的所有关联,并根据导入数据重新创建它们。

How can I do this? 我怎样才能做到这一点?

I was thinking of getting all associated entities, and then call disassociate for each of them, but I got stuck trying to get those associated entities. 我当时想获取所有关联的实体,然后为它们中的每一个调用Disassociate,但是我在尝试获取那些关联的实体时遇到了麻烦。

How should I approach this? 我应该如何处理?

Suppose that you have Student Entity And you want to copy the student Cources to another custom Entity as you said named CStudent 假设您有学生实体,并且要将学生课程复制到另一个名为CStudent的自定义实体
You can use the following code: 您可以使用以下代码:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM