简体   繁体   English

MS Dynamics CRM将记录添加到C#中的a1:N分配中

[英]MS dynamics CRM add record to a1:N realtionship within C#

I have CRM entity called C which has N:1 relationship with entity Account. 我有一个称为C的CRM实体,它与实体帐户具有N:1的关系。 Account has 1:N relationship with entity B 帐户与实体B有1:N关系

Problem Statement: 问题陈述:

Create a new record for Entity C with update of existing Account(a) (Update in existing account(a) is : a already had two records b1 and b2 of type Entity B . I need to add third record b3 of type Entity B ) 使用现有帐户(a)的更新实体C创建新记录 (现有帐户(a)中的更新为: a已经具有两个实体B类型的记录b1和b2。 我需要添加实体B类型的第三个记录b3

Following is my code 以下是我的代码

_context.Attach(c.relationshipNameWithAccount);
_context.UpdateObject(c.relationshipNameWithAccount);
_context.AddObject(c);
_context.SaveChanges();

This code is executing successfully. 该代码正在成功执行。

It is Adding a new record in C (as expected) also updating fields in existing record a (as expected) but not adding b3 in existing account a (not expected). 它正在C中 添加一个新记录(按预期方式),还更新了现有记录a中的字段 (按预期方式),但没有在现有帐户a中 添加 b3(不预期)。

You Can Use AssociateRequest for Your Association like this 您可以像这样对您的关联使用AssociateRequest

EntityReferenceCollection relatedEntities = new EntityReferenceCollection(); EntityReferenceCollection relatedEntities =新的EntityReferenceCollection();

relatedEntities.Add(new EntityReference(B, BId)); relatedEntities.Add(new EntityReference(B,BId));

Relationship relationship = new Relationship("B_RelationName_A"); 关系关系=新的Relationship(“ B_RelationName_A”);

_service.Associate(A, AId, relationship, relatedEntities); _service.Associate(A,AId,关系,relatedEntities);

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

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