locked
OrganizationServiceContext SaveChanges() method throws server exception RRS feed

  • Question

  • I am trying to update a record using ServiceContext as follows:

    First I find the record I’m looking for depending on an attribute:

    var someRecord = serviceContext.CreateQuery(“someEntity”)

                       .FirstOrDefault(s => s.GetAttributeValue<string>("new_name") == someNameVar);

     

     

    I want to update the attribute

    someRecord["new_name"] = “updated value for name";

     

    if (!serviceContext.IsAttached(someRecord))

         serviceContext.Attach(someRecord);

     

    serviceContext.UpdateObject(someRecord);

    serviceContext.SaveChanges();

     

     

    Everything works until I hit the SaveChanges() call. At this point I get an exception that there was an error on the server. When I check the server log here is one of the exceptions that I get (that gives the most info):

    [2011-10-25 18:07:38.878] Process: w3wp |Organization:00000000-0000-0000-0000-000000000000 |Thread:   23 |Category: Platform |User: 00000000-0000-0000-0000-000000000000 |Level: Error | ExceptionConverter.ConvertMessageAndErrorCode

    >System.InvalidOperationException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #E97FE62A: System.InvalidOperationException: The entity is read-only and the 'Id' property cannot be modified. Use the context to update the entity instead.

    >   at Microsoft.Xrm.Sdk.Entity.set_Id(Guid value)

    >   at Microsoft.Crm.Extensibility.ProxyPluginExecutionContext.CopyModifiedEntityAttributes(PipelineConversionContext context, Entity originalEntity, Entity newEntity)

    >   at Microsoft.Crm.Extensibility.ProxyPluginExecutionContext.ConvertPropertyBagToParameterCollection(PipelineExecutionContext context, PropertyBag propertyBag, ParameterCollection originalParameterCollection)

    >   at Microsoft.Crm.Extensibility.ProxyPluginExecutionContext.Dispose(Boolean disposing)

    >   at Microsoft.Crm.Extensibility.VersionedPluginProxyStepBase.GetImages(PipelineExecutionContext context, Object pluginDefinition)

    >   at Microsoft.Crm.Extensibility.ImageRetrievalStep.MergeEntityRequests(PipelineExecutionContext context, Dictionary`2 entityRequests)

    >   at Microsoft.Crm.Extensibility.ImageRetrievalStep.Execute(PipelineExecutionContext context)

    >   at Microsoft.Crm.Extensibility.Pipeline.Execute(PipelineExecutionContext context)

    >   at Microsoft.Crm.Extensibility.MessageProcessor.Execute(PipelineExecutionContext context)

    >   at Microsoft.Crm.Extensibility.InternalMessageDispatcher.Execute(PipelineExecutionContext context)

    >   at Microsoft.Crm.Extensibility.ExternalMessageDispatcher.ExecuteInternal(IInProcessOrganizationServiceFactory serviceFactory, IPlatformMessageDispatcherFactory dispatcherFactory, String messageName, String requestName, Int32 primaryObjectTypeCode, Int32 secondaryObjectTypeCode, ParameterCollection fields, CorrelationToken correlationToken, CallerOriginToken originToken, UserAuth userAuth, Guid callerId, Guid transactionContextId, Int32 invocationSource, Nullable`1 requestId, Version endpointVersion)

    >   at Microsoft.Crm.Extensibility.ExternalMessageDispatcher.Execute(IInProcessOrganizationServiceFactory serviceFactory, IPlatformMessageDispatcherFactory dispatcherFactory, String messageName, String requestName, Int32 primaryObjectTypeCode, Int32 secondaryObjectTypeCode, ParameterCollection fields, CorrelationToken correlationToken, CallerOriginToken originToken, UserAuth userAuth, Guid callerId, Guid transactionContextId, Int32 invocationSource, Nullable`1 requestId)

    >   at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.ExecuteRequest(OrganizationRequest request, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType)

    >   at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Execute(OrganizationRequest request, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType)

     

    Have not been able to make headway on this - any ideas on what could be causing this would be appreciated.

    -- Enrico

    Tuesday, October 25, 2011 10:42 PM

Answers

All replies

  • Hi Mate

    check if somerecord has any value and of what type?

    var someRecord = serviceContext.CreateQuery(“someEntity”)

                       .FirstOrDefault(s => s.GetAttributeValue<string>("new_name") == someNameVar);

    may be you need to change the following line

    .FirstOrDefault(s => s.GetAttributeValue<string>("new_name") == someNameVar).ToEntity();

    I hope this helps.


    Amreek Singh Senior CRM Consultant CDC Praxa Sydney, Australia http://mscrmshop.blogspot.com http://crm2011usersettings.codeplex.com
    • Proposed as answer by Amreek Singh Tuesday, October 25, 2011 11:42 PM
    Tuesday, October 25, 2011 11:42 PM
  • Make sure you do not have to manually attach the object to the context. Since you have just read the record, context should already be tracking it. 

    I guess since you are attaching it to conext manually, it may be throwing that error.

     

    HTH

    Sam


    Web: http://www.inogic.com
    Blog: http://inogic.blogspot.com
    Email: news@inogic.com
    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    • Proposed as answer by Sam - Inogic Wednesday, October 26, 2011 12:33 AM
    Wednesday, October 26, 2011 12:33 AM
  • Thanks Amreek -

    There is no .ToEntity() method - there's a .ToEntity<entityType>() method - but since I'm working with late binding I don't have access to a fixed class for my entity. That being said, the someRecord variable does get returned as an Entity object and I have access to all of the properties of it - including Id and all attributes.

    -- Enrico

    Wednesday, October 26, 2011 12:53 AM
  • Thanks Sam - yeah, that's not the case. Walking through the code the line:

         serviceContext.Attach(someRecord);

    doesn't actually get called so this isn't the issue.

    Really stumped with this one...

    -- Enrico

     

    Wednesday, October 26, 2011 12:54 AM
  • Hi Enrico - did you ever get an answer to this? I'm struggling with exactly the same problem. Completely Stumped... James
    Monday, January 30, 2012 7:46 AM
  • Hi James -

    No I did not - I did the coding without using any LINQ constructs.

    Enrico

    Monday, January 30, 2012 8:57 PM
  • That's unfortunate. It seems the problem is specifically in plug-ins - we did a test using identical LINQ and context.Update() code in a plug-in and in an external web-site, and the same identical code ran in the website but threw that Exception in the plug-in. We haven't tried creating a new context from scratch in a plug-in and using that yet - we've run out of time so I'm just going to use the API in the standard way too.

     

    James  


    -- James
    Tuesday, January 31, 2012 4:06 AM
  • Wednesday, May 1, 2013 8:03 PM