2012-01-10 59 views
4

如何在crm 2011中创建和删除多对多实体关系中的数据?如何在CRM 2011中创建和删除多对多实体关系中的数据?

代码:

QueryExpression qry = new QueryExpression(); 
qry.EntityName = "entity1_entity2"; 
qry.ColumnSet = new ColumnSet(true); 

var re = crmservice.RetrieveMultiple(qry).Entities; 


crmservice.Delete("entity1_entity2", re[0].Id); 

的FaultException:The 'Delete' method does not support entities of type 'entity1_entity2'.

回答

5

为了通过N到连接两个记录:N的关系,已使用Associate/Disassociate请求或服务代理的相应方法。

这将创建/删除entity1_entity2实体的相应记录。

4
using Microsoft.Crm.Sdk.Messages; 
... 
// get the crm service 
... 
AssociateEntitiesRequest fooToBar = new AssociateEntitiesRequest 
{ 
    Moniker1 = foo,    // foo is an entity reference 
    Moniker2 = bar,    // bar is an entity reference 
    RelationshipName = "foo_bar", // name of the relationship 
} 

service.Execute(fooToBar)   // relates foo and bar 

这里有一个博客帖子:http://charithrajapaksha.blogspot.com/2011/08/creating-many-to-many-records-in-crm.html

0

在N:N关系的记录应关联和关联。您无法在N:N关系中创建和删除记录。你可以使用AssociateRequest,DisassociateRequest类,或者你可以在Plugin Registration工具中使用Associate,Disassociate Messages。

2

对于删除您尝试以下

 // Create an AssociateEntities request. 
     //Namespace is Microsoft.Crm.Sdk.Messages 
     DisassociateEntitiesRequest request = new DisassociateEntitiesRequest(); 

     // Set the ID of Moniker1 to the ID of the lead. 
     request.Moniker1 = new EntityReference 
     { 
      Id = moniker1.Id, 
      LogicalName = moniker1.Name 
     }; 

     // Set the ID of Moniker2 to the ID of the contact. 
     request.Moniker2 = new EntityReference 
     { 
      Id = moniker2.Id, 
      LogicalName = moniker2.Name 
     }; 

     // Set the relationship name to associate on. 
     request.RelationshipName = strEntityRelationshipName; 

     // Execute the request. 
     service.Execute(request); 
相关问题