2

我需要創建/定義一個潛在客戶和一個自定義實體之間以及一個聯繫人和一個自定義實體之間的多對多關係。我似乎無法找到我想要做的任何代碼示例。在C#中的CRM 4和5中創建多對多關係

這需要在兩者CRM 4和CRM 5.

是否有任何缺點做兩個N的工作:1的關係,而不是N:N的關係?

+0

是否有任何缺點取決於關係應該是關於什麼。通常,n:m關係有點受限,因爲沒有辦法記錄關於兩個記錄之間連接的任何附加數據。他們有時候會有一點點,但總的來說在使用上並不那麼簡單。 – TeaDrivenDev 2011-06-02 22:16:43

回答

0

您可以通過轉到實體N:N關係,通過UI執行N:N關係。與一箇中間實體在執行單個N:N時建立2個N:1關係是有利的,例如您可以存儲關係屬性(關係的角色),還可以通過雙N:1關係鏈接工作流。

編輯:通過代碼,你可以參考CRM 2011 SDK幫助頁「創建和檢索實體關係」,摘錄如下ñ關係:

要創建n個。您將能夠通過在4.0元數據服務類同做點什麼 -

創建N:N實體關係

下面的示例使用EligibleCreateManyToManyRelationship方法來驗證帳戶和廣告活動的實體可以參與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

下面的示例將創建一個使用CanManyToManyRequest來驗證實體是否能夠參與N A EligibleCreateManyToManyRelationship方法: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; 
} 
+0

我需要在代碼中專門做C#。我一直在玩弄通過UI創建關係,試圖弄清楚是否值得在N:N中添加或者只是做兩個N:1。 – Justin 2011-06-01 19:18:34

+0

謝謝科爾!由於我們需要保留一些信息以供稍後顯示,看起來我們將採用幾種N:1關係。 – Justin 2011-06-03 13:29:22

+0

沒問題!雙N:1是我在大多數情況下使用的。 – cchamberlain 2011-06-03 14:37:22