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

4

有没有人知道如何使用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!" );
}

感谢您的所有帮助 :)
2个回答

6

试试这个:

pet["foodtype"] = "Seaweed";

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

编辑: "上下文当前未跟踪'宠物'实体"的意思是您从检索中获取的对象未附加到服务上下文。有一个名为Attach的方法可以做到这一点。

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

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

在UpdateObject上出现错误:“上下文当前未跟踪'pet'实体”。 - CompanyDroneFromSector7G
我已经找到了一个解决方案(见下文),但是你的答案很有教育意义,而且它也有效,所以我接受了你的答案。感谢你的帮助 :) - CompanyDroneFromSector7G

2

这是有效的:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接