简体   繁体   English

C#CRM m:n关系

[英]C# CRM m:n relationship

In my CRM database, I have two entity groups, A and B, linked by an m:n-relationship. 在我的CRM数据库中,我有两个实体组A和B,它们通过m:n关系链接。 There are many instances of entity A each linked to several instances of entity B. For a particular entity, let's say an an instance of entity A, how do I get all the linked instances of entity B? 实体A的实例很多,每个实例都链接到实体B的几个实例。对于一个特定的实体,假设一个实体A的实例,我如何获得实体B的所有链接实例?

var service = new OrganizationService(_connection);

var retrieveRequest = new RetrieveMultipleRequest();

retrieveRequest.Query = new QueryExpression
    {
        EntityName = "new_A",
        ColumnSet = new ColumnSet(true)
    }; 

var crmReponse = (RetrieveMultipleResponse)service.Execute(retrieveRequest); 

When viewing crmResponse in debugging mode, I see all instances of entity A, but I do not see anything within the RelatedInstances property. 在调试模式下查看crmResponse时,我看到了实体A的所有实例,但是在RelatedInstances属性内没有看到任何内容。 What am I doing wrong? 我究竟做错了什么?

EDIT: After getting the first answer, I adapted my code as follows: 编辑:在得到第一个答案后,我将代码修改如下:

var linkEntities = new LinkEntity { 

    LinkFromEntityName = "new_A",
    LinkToEntityName = "new_B"

};

var retrieveRequest = new RetrieveMultipleRequest();

var query = new QueryExpression
{
    EntityName = "new_A",
    ColumnSet = new ColumnSet(true)
};

query.LinkEntities.Add(linkEntities);

retrieveRequest.Query = query;

var crmReponse = (RetrieveMultipleResponse)service.Execute(retrieveRequest); 

Now I get an error, though: 现在,我得到一个错误:

No system many-to-many relationship exists between new_customer_system_machine_type and new_customer_system_machine_check. new_customer_system_machine_type和new_customer_system_machine_check之间不存在系统多对多关系。 If attempting to link through a custom many-to-many relationship ensure that you provide the from and to attributes. 如果尝试通过自定义的多对多关系进行链接,请确保提供from和to属性。

However, when I created that many-to-many-relationship, I did not specify any attribute names, it was merely between the entities. 但是,当我创建多对多关系时,我没有指定任何属性名称,而只是在实体之间。 What am I doing wrong? 我究竟做错了什么?

Also, my RetrieveMultipleRequest class does not have the property ReturnDynamicEntities 另外,我的RetrieveMultipleRequest类没有属性ReturnDynamicEntities

You need to specify the LinkFromAttributeName and the LinkToAttributeName. 您需要指定LinkFromAttributeName和LinkToAttributeName。 These are going to be the attributes of the from and to entities that contain the matching key values. 这些将成为包含匹配键值的from和to实体的属性。

You can find answer in this blog post . 您可以在此博客文章中找到答案。 There described two ways: 描述了两种方法:
- RetrieveMultipleRequest with LinkEntity class; - RetrieveMultipleRequestLinkEntity类;
- Fetch - 获取

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

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