MS Dynamics CRM 2011 SDK - Update entity record using late-binding

感情迁移 提交于 2019-12-07 11:02:37

问题


does anyone know how to save changes to a late-bound entity using the SDK for Dynamics CRM 2011?

This is what I've tried:

// 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!" );
}

Thanks for all help :)


回答1:


Try this:

pet["foodtype"] = "Seaweed";

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

EDIT: "The context is not currently tracking the 'pet' entity" means the object you get from Retrieve is not attached to the service context. There's a method Attach that does just that.

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

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



回答2:


This works:

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


来源:https://stackoverflow.com/questions/11455592/ms-dynamics-crm-2011-sdk-update-entity-record-using-late-binding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!