MS Dynamics CRM 2011 SDK - 使用后期绑定更新实体记录

问题描述:

没有人知道如何使用SDK for Dynamics CRM 2011将更改保存到后期绑定的实体?MS Dynamics CRM 2011 SDK - 使用后期绑定更新实体记录

这是我已经试过:

// retrieve and modify a pet... 
// (This part works) 
Guid findId = new Guid("6CA57D73-30CC-E111-B155-00505630052F"); 
ColumnSet attributes = new ColumnSet(new string[] { "name", "foodtype" }); 

// try to retrieve 
// (this also works) 
pet = xrm.Retrieve("pet", findId, attributes); 
if(pet!=null) 
{ 
    Console.WriteLine(String.Format("Retrieved pet {0} successfully!", pet["name"].ToString())); 
    // update attributes 
    pet["foodtype"] = "Seaweed"; 
    // (from here doesn't seem to work) 
    // save pet 
    xrm.SaveChanges(); 
    Console.WriteLine("Done!"); 
} 

感谢所有帮助:)

+0

这是否养有什么错误? – 2012-07-12 16:10:19

试试这个:

pet["foodtype"] = "Seaweed"; 

xrm.UpdateObject(pet); 
xrm.SaveChanges(); 

编辑:"The context is not currently tracking the 'pet' entity"意味着你从Retrieve获取对象是不附加到服务上下文。有一种方法Attach就是这么做的。

xrm.Attach(pet); 
pet["foodtype"] = "Seaweed"; 

xrm.UpdateObject(pet); 
xrm.SaveChanges(); 
+0

这在UpdateObject上给出了一个错误:“上下文当前没有跟踪'宠物'实体”。 – CompanyDroneFromSector7G 2012-07-12 15:57:58

+0

我已经找到了一个解决方案(见下文),但你的答案是教育性的,它的工作原理,所以我接受你的答案。谢谢您的帮助 :) – CompanyDroneFromSector7G 2012-07-13 09:04:18

这工作:

pet["foodtype"] = "Seaweed"; 
pet.EntityState = EntityState.Changed; // not sure if this is really needed 
// save pet 
xrm.Update(pet);