2012-08-10 167 views
1

我在使用LINQ查找記錄並更新找到的記錄時遇到了問題。即使這個超級簡單的版本也不行。有錯誤是完全通用的(見下文)。CRM 2011 LINQ更新錯誤

下面是代碼,我從查詢中得到一個實體,我無法更新它。

var account = orgContext.CreateQuery("account").First(c => c["name"] == "apple"); 

account["name"] = "Microsoft"; 

orgContext.UpdateObject(account); 
orgContext.SaveChanges(); ///ERROR HERE 




ERROR DETAIL 

Microsoft.Xrm.Sdk.SaveChangesException was unhandled by user code 
    Message=An error occured while processing this request. 
    Source=Microsoft.Xrm.Sdk 
    StackTrace: 
     at Microsoft.Xrm.Sdk.Client.OrganizationServiceContext.SaveChanges(SaveChangesOptions options) 
     at Dhs.Tsa.Trip.Xrm.Plugins.ProcessNFL.Execute(IServiceProvider serviceProvider) in C:\Users\Administrator\Desktop\Dhs.Tsa.Trip.Xrm\Dhs.Tsa.Trip.Xrm.Plugins\ProcessNFL.cs:line 54 
     at Microsoft.Crm.Extensibility.V5PluginProxyStep.ExecuteInternal(PipelineExecutionContext context) 
     at Microsoft.Crm.Extensibility.VersionedPluginProxyStepBase.Execute(PipelineExecutionContext context) 
    InnerException: System.ServiceModel.FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> 
     Message=System.InvalidOperationException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #97345966 
     Source=Microsoft.Crm.Extensibility 
     StackTrace: 
      at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Execute(OrganizationRequest request, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType) 
      at Microsoft.Crm.Extensibility.InprocessServiceProxy.ExecuteCore(OrganizationRequest request) 
      at Microsoft.Xrm.Sdk.Client.OrganizationServiceContext.Execute(OrganizationRequest request) 
      at Microsoft.Xrm.Sdk.Client.OrganizationServiceContext.SaveChange(OrganizationRequest request, IList`1 results) 
     InnerException: 
+0

您是否嘗試過使用'account.name =「Microsoft」;'? – 2012-08-10 01:52:16

+0

你可以發佈其他的例外細節嗎?它在「內部例外」中刪除 - 是否沒有更多細節要添加? – glosrob 2012-08-10 14:45:22

+0

[this](http://blogs.msdn.com/b/madenwal/archive/2011/03/22/crm-2011-sdk-error-while-using-the-create-method-system-argumentnullexception-value -cannot-be-null.aspx)可能是相關的 – keerz 2012-08-10 21:31:10

回答

1

我不是100%確定那個錯誤指的是什麼,但我知道在過去,我嘗試使用與檢索到的對象一起更新時遇到問題。例如,如果您提取的對象缺少必填字段,則可能不希望您嘗試再次將其保存回去。

無論如何,解決方案很簡單。而不是...

account["name"] = "Microsoft"; 

orgContext.UpdateObject(account); 
orgContext.SaveChanges(); 

嘗試做...

var updAccount = new Entity("account") { Id = account.Id }; 
updAccount["name"] = "Microsoft"; 

orgContext.UpdateObject(updAccount); 
orgContext.SaveChanges(); 

這有效地創建帳戶引用的副本,只有你想在它更新的字段。