假設你有學生實體,要學生Cources複製到另一個自定義實體如你所說命名CStudent
您可以使用下面的代碼:
var scs = Context.new_Student_CourcesSet.Where(x => x.new_courceid == Cource.Id).ToList<new_Student_Cources>();
var removedsc = Context.new_new_CStudent_CourcesSet.Where(x => x.new_cstudentid == CStudent.Id).ToList<new_CStudent_Cources>();
EntityReferenceCollection relatedEntities = new EntityReferenceCollection();
EntityReferenceCollection removedrelatedEntities = new EntityReferenceCollection();
Relationship relationship = new Relationship(new_CStudent_Cources.EntityLogicalName);
if (removedsc != null)
{
foreach (new_CStudent_Cources c in removedsc)
{
RemovedrelatedEntities.Add(new EntityReference(new_Cources.EntityLogicalName, (Guid)ar.new_courcesid));
}
Service.Disassociate(CStudent.LogicalName, CStudnetid, relationship, RemovedrelatedEntities);
}
foreach (new_Student_Cources d in scs)
{
relatedEntities.Add(new EntityReference(new_Cources.EntityLogicalName, (Guid)d.new_courceid));
}
Service.Associate(CStudent.LogicalName, CStudentid, relationship, relatedEntities);
我想我已經找到了解決辦法: – user1412690
http://mscrmkb.blogspot.com/2010/12/crm-2011-retrieve-related-entity-data.html – user1412690