CRM 2015自定义工作流活动,获取错误:实体是只读的,并且“ID&ID”是属性无法修改

时间:2016-02-26 15:25:45

标签: dynamics-crm-2015

我在使自定义工作流活动工作时遇到问题。我把它剥离回最简单的代码:

// TODO: Implement your custom Workflow business logic.

// Get the context
var xrmContext = new OrganizationServiceContext(service);

// Get an object that we can update
var client = (from a in xrmContext.CreateQuery("account")
    where a.GetAttributeValue<Guid>("accountid") == new Guid("bfa273d1-d34c-e511-80cc-00155d021c08")
    select a).Single();

// Make a change
client.Attributes["name"] = "Gray Test A - CHANGED";

// Write it out
xrmContext.UpdateObject(client);
xrmContext.SaveChanges();

如果您已创建工作流程,您会发现它并不比这简单得多:

  • 获取上下文
  • 获取帐户记录
  • 进行最简单的更改
  • 更新并保存。

我在跟踪日志中收到以下错误:

[2016-02-26 15:15:09.328] Process:CrmAsyncService |Organization:00000000-0000-0000-0000-000000000000 |Thread:   36 |Category: Platform |User: 00000000-0000-0000-0000-000000000000 |Level: Error |ReqId: 00000000-0000-0000-0000-000000000000 | ExceptionConverter.ConvertMessageAndErrorCode  ilOffset = 0x23B
>   System.InvalidOperationException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #5CC91FB6: 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.PipelineInstrumentationHelper.Execute(Boolean instrumentationEnabled, String stopwatchName, ExecuteWithInstrumentation action)
>   at Microsoft.Crm.Extensibility.Pipeline.Execute(PipelineExecutionContext context)
>   at Microsoft.Crm.Extensibility.MessageProcessor.<>c__DisplayClass1.<RunStage>b__0()
>   at Microsoft.Crm.Extensibility.PipelineInstrumentationHelper.Execute(Boolean instrumentationEnabled, String stopwatchName, ExecuteWithInstrumentation action)
>   at Microsoft.Crm.Extensibility.MessageProcessor.RunStage(PipelineExecutionContext context, Int32 pipelineStage)
>   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.OrganizationSdkServiceInternal.ExecuteRequestRequestWithInstrumentation(OrganizationRequest request, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType, UserAuth userAuth, Guid targetUserId, OrganizationContext context, Boolean returnResponse, Boolean checkAdminMode, Object operation)
>   at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.ExecuteRequest(OrganizationRequest request, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType, UserAuth userAuth, Guid targetUserId, OrganizationContext context, Boolean returnResponse, Boolean checkAdminMode)
>   at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.ExecuteRequest(OrganizationRequest request, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType, Boolean checkAdminMode)
>   at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Execute(OrganizationRequest request, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType, Boolean checkAdminMode)

我无法理解为什么我不能做这个最简单的操作。

任何人都可以光明吗? 谢谢, 灰色

1 个答案:

答案 0 :(得分:1)

尝试在保存之前将记录附加到上下文,如下所示:

xrmContext.ClearChanges();
if (!xrmContext.IsAttached(client)) xrmContext.Attach(client);
xrmContext.UpdateObject(client);
xrmContext.SaveChanges();