web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

crm plugin The entity is read-only and the 'EntityState' property cannot be modified. Use the context to update the entity instead

(1) ShareShare
ReportReport
Posted on by 395

Hi,

My code:

myEntity= (from t in xrmContext.MyEntitySet
where t.myEntityId == myEntityId .lookUpField.Id
select new MyEntity()
{
myEntityId = t.myEntityId,
field1=t.field
}).FirstOrDefault();
myEntity.EntityState = EntityState.Changed;
service.Update(myEntity);

In the line myEntity.EntityState = EntityState.Changed; 

I get an error: The entity is read-only and the 'EntityState' property cannot be modified. Use the context to update the entity instead.

What should I do?

thanks!

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    BackToTheCrm Profile Picture
    on at
    RE: crm plugin The entity is read-only and the 'EntityState' property cannot be modified. Use the context to update the entity instead

    Hello Nomi,

    To update an record check this example :

    msdn.microsoft.com/.../gg328416.aspx

  • NAOMI314 Profile Picture
    395 on at
    RE: crm plugin The entity is read-only and the 'EntityState' property cannot be modified. Use the context to update the entity instead

    thanks!

    I use early bound,

    Your link refers to late bound.

    What I  need is how to change the EntityState.

    thanks!!!

  • Suggested answer
    ManirajKV Profile Picture
    1,477 on at
    RE: crm plugin The entity is read-only and the 'EntityState' property cannot be modified. Use the context to update the entity instead

    Hi,

    Instead of using "Service.Update", try to create context like below and check

    ---------------------------------------------

    using Microsoft.Xrm.Client;

    using (var service = new OrganizationService(connection))

    using (var context = new OrganizationServiceContext(service))

    {

    var contact = context.CreateQuery<Contact>().First(c => c.FirstName == "Bob");

    contact.JobTitle = "Developer";

    context.UpdateObject(contact);

    context.SaveChanges();

    // contact is no longer attached at this point so reattach it

    context.Reattach(contact);

    contact.EMailAddress1 = "bob@contoso.com";

    context.UpdateObject(contact);

    context.SaveChanges();

    }

    ----------------------------------------------------------------------

    Hope it helps. If u facing any issue, please reply back

  • Suggested answer
    BackToTheCrm Profile Picture
    on at
    RE: crm plugin The entity is read-only and the 'EntityState' property cannot be modified. Use the context to update the entity instead

    Nomi,

    Here is the example for early-bound : https://msdn.microsoft.com/en-us/library/gg334754.aspx

    Do you mean you want to update the State Code ?

    If it's the case you must use the SetStateRequest message, here is an example : https://msdn.microsoft.com/en-us/library/hh547420.aspx

    If you're in CRM 2015 online update 1, you can directly set the StateCode property and the corresponding StatusCode property.

  • NAOMI314 Profile Picture
    395 on at
    RE: crm plugin The entity is read-only and the 'EntityState' property cannot be modified. Use the context to update the entity instead

    Thank u very mach!!

    I am in CRM 2015 online update 1, So how can I directly set the StateCode property and the corresponding StatusCode property?

    u mean :myEntity.EntityState = EntityState.Changed;?

    thanks!

  • Verified answer
    BackToTheCrm Profile Picture
    on at
    RE: crm plugin The entity is read-only and the 'EntityState' property cannot be modified. Use the context to update the entity instead

    Nomi,

    StateCode is different from EntityState, the state of a record is defined by the combination of the StateCode and the StatusCode.

    EntityState is a read-only property which indicate whether or not the entity object as been modified in the C# code and need to be updated.

    I think you mistake the 2 definitions.

    So, if you want to change the Status (Active/Inactive), you must use the StateCode and the StatusCode properties.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#2
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans