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

Chad picture Chad · Feb 22, 2011 · Viewed 16.6k times · Source

We are able to create new entities without any issues, but updating an existing entity in a plugin this does not appear to be working. This is for CRM 2011.

var crmContext = new CustomCrmContext(service);

var contact = crmContext.Contact.FirstOrDefault(c=>c.Id == targetEntity.Id);

contact.new_CustomField = "Updated";

crmContext.SaveChanges();

Answer

Josh Painter picture Josh Painter · May 25, 2011

No need to download the whole Contact record if you already have the Id and you just need to update a field or two. You also don't need the OrganizationServiceContext - just the Service. Try something like:

var c = new contact() {
  Id = targetEntity.Id,
  new_CustomField = "Updated"
}

service.Update(c);

This will save the roundtrip of querying for the contact first.