【问题标题】:Create a Many to Many relationship in CRM 4 and 5 in C#在 C# 中在 CRM 4 和 5 中创建多对多关系
【发布时间】:2011-09-06 08:04:46
【问题描述】:

我需要在潜在客户和自定义实体之间以及联系人和自定义实体之间创建/定义多对多关系。我似乎找不到任何我想做的代码示例。

这需要在 CRM 4 和 CRM 5 中都有效。

做两个 N:1 关系而不是 N:N 关系有什么缺点吗?

【问题讨论】:

  • 是否有任何缺点取决于关系应该是什么。通常,n:m 关系受到更多限制,因为无法记录有关两条记录之间连接的任何附加数据。它们有时会有点,但总体而言,它们的使用并没有简单得多。

标签: many-to-many entity-relationship dynamics-crm-4 relationship crm


【解决方案1】:

您可以通过 UI 转到实体 N:N 关系来建立 N:N 关系。与中间实体进行 2 N:1 关系比进行单个 N:N 具有优势,例如您可以存储关系属性(关系的角色),并且您可以通过双重 N:1 关系链接工作流。

编辑:

要通过代码创建 N:N 关系,您可以参考 CRM 2011 SDK 帮助页面“创建和检索实体关系”,摘录如下。您将能够通过 4.0 中的元数据服务做类似的事情 -

创建 N:N 实体关系

以下示例使用 EligibleCreateManyToManyRelationship 方法验证 Account 和 Campaign 实体是否可以参与 N:N 实体关系,然后使用 CreateManyToManyRequest 创建实体关系。

bool accountEligibleParticipate =
    EligibleCreateManyToManyRelationship("account");
bool campaignEligibleParticipate =
    EligibleCreateManyToManyRelationship("campaign");

if (accountEligibleParticipate && campaignEligibleParticipate)
{

    CreateManyToManyRequest createManyToManyRelationshipRequest =
        new CreateManyToManyRequest
    {
        IntersectEntitySchemaName = "new_accounts_campaigns",
        ManyToManyRelationship = new ManyToManyRelationshipMetadata
        {
            SchemaName = "new_accounts_campaigns",
            Entity1LogicalName = "account",
            Entity1AssociatedMenuConfiguration =
            new AssociatedMenuConfiguration
            {
                Behavior = AssociatedMenuBehavior.UseLabel,
                Group = AssociatedMenuGroup.Details,
                Label = new Label("Account", 1033),
                Order = 10000
            },
            Entity2LogicalName = "campaign",
            Entity2AssociatedMenuConfiguration =
            new AssociatedMenuConfiguration
            {
                Behavior = AssociatedMenuBehavior.UseLabel,
                Group = AssociatedMenuGroup.Details,
                Label = new Label("Campaign", 1033),
                Order = 10000
            }
        }
    };

    CreateManyToManyResponse createManytoManyRelationshipResponse =
        (CreateManyToManyResponse)_serviceProxy.Execute(
        createManyToManyRelationshipRequest);


    _manyToManyRelationshipId =
        createManytoManyRelationshipResponse.ManyToManyRelationshipId;
    _manyToManyRelationshipName =
        createManyToManyRelationshipRequest.ManyToManyRelationship.SchemaName;

    Console.WriteLine(
        "The many-to-many relationship has been created between {0} and {1}.",
        "account", "campaign");
}

EligibleCreateManyToManyRelationship

以下示例创建一个 EligibleCreateManyToManyRelationship 方法,该方法使用 CanManyToManyRequest 来验证实体是否可以参与 N:N 实体关系。

/// <summary>
/// Determines whether the entity can participate in a many-to-many relationship.
/// </summary>
/// <param name="entity">Entity</param>
/// <returns></returns>
public bool EligibleCreateManyToManyRelationship(string entity)
{
    CanManyToManyRequest canManyToManyRequest = new CanManyToManyRequest
    {
        EntityName = entity
    };

    CanManyToManyResponse canManyToManyResponse =
        (CanManyToManyResponse)_serviceProxy.Execute(canManyToManyRequest);

    if (!canManyToManyResponse.CanManyToMany)
    {
        Console.WriteLine(
            "Entity {0} can't participate in a many-to-many relationship.", 
            entity);
    }

    return canManyToManyResponse.CanManyToMany;
}

【讨论】:

  • 我需要在代码中完成,特别是 C#。我整个早上都在尝试通过 UI 创建关系,试图弄清楚添加 N:N 还是只做两个 N:1 是否值得。
  • 谢谢科尔!看起来我们将处理几个 N:1 关系,因为我们需要保留一些信息以便稍后显示。
  • 没问题! Double N:1 是我在大多数情况下使用的。
猜你喜欢
  • 1970-01-01
  • 2013-09-05
  • 1970-01-01
  • 2013-03-30
  • 2011-07-04
  • 1970-01-01
  • 1970-01-01
  • 2020-07-06
相关资源
最近更新 更多