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

时间:2012-01-10 11:49:02

标签: c# web-services dynamics-crm dynamics-crm-2011 crm

如何在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'.

4 个答案:

答案 0 :(得分:5)

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

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

答案 1 :(得分: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

答案 2 :(得分:3)

对于删除,请尝试以下

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

答案 3 :(得分:0)

在N:N关系中,记录应该关联和解除关联。您无法在N:N关系中创建和删除记录。您可以使用AssociateRequest,DisassociateRequest类,也可以在插件注册工具中使用Associate,Disassociate Messages。