“EntityState must be set to null, Created (for Create message) or Changed (for Update message)” when attempting to update an entity in CRM 2011

不羁的心 提交于 2019-11-29 09:15:56
ccellar

You have to tell your crmContext (use appropriate name) what to do with the changes.

You should add crmContext.UpdateObject(contact); before crmContext.SaveChanges();

See also How to update a CRM 2011 Entity using LINQ in a Plugin?

I had the same problem. I switched from using

context.Update(object) 

to

context.UpdateObject(object) 

and it worked.

To avoid the problem you can simply use update-helper-objects instead of using the retrieved record:

var policyUpdater = new Policy { Id = _policy.Id, FieldToUpdate = "newValue" };
service.Update(policyUpdater);

Note: Properties of the update-helper-object that aren't set are simply ignored. The update won't set the corresponding record fields to null

This worked for me:

recordToUpdate.EntityState = EntityState.Changed;

(recordToUpdate is an Entity to be updated)

Turns out it was an issue with my linq query that was retrieving the entity in the first place. When I replaced this with a query expression it worked okay.

Time to brush up on my linq!

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