2009-09-09 73 views
0

我有一個項目設置 - 它是託管在ASP.Net站點中的Silverlight客戶端應用程序。它有一個ADO.Net實體框架來與SQL Server數據庫和ADO.Net數據服務進行通信。 我有一些麻煩讓我的異步CRUD Silverlight插入在我的數據庫上工作。第一種方法正常觸發並傳入URI。但是當「OnClientJobQueryComplete」方法觸發時,它失敗了大約5行,我不明白爲什麼。例外說「處理此請求時發生錯誤」。Silverlight - Crud插入失敗意外?

private void addStuff_Click(object sender, RoutedEventArgs e) 
    { 
     // Define a URI that returns the product with the specified ID. 
     Uri jobrefUri = new Uri(svcContext.BaseUri.AbsoluteUri 
      + "/ClientJob(" + this.jobref.Text + ")"); 

     // Begin a query operation retrieve the Product object 
     // that is required to add a link to the new Order_Detail. 
     svcContext.BeginExecute<ClientJob>(jobrefUri, 
      OnClientJobQueryCompleted,null); 
    } 

    private void OnClientJobQueryCompleted(IAsyncResult result) 
    { 
     // Use the Dispatcher to ensure that the 
     // asynchronous call returns in the correct thread. 
     Dispatcher.BeginInvoke(() => 
     { 
      // Get the Product returned by the completed query. 
      IEnumerable<ClientJob> queryResult = 
       svcContext.EndExecute<ClientJob>(result);//**TRIES THIS BUT FAILS HERE 

      ClientJob returnedClientJob = queryResult.First(); 

      // Get the currently selected order. (Create new Guid since not Northwind) 
      Guid g = Guid.NewGuid(); 

      // Create a new Order_Details object with the supplied FK values. 
      Job newItem = Job.CreateJob(g); 
      //Job newItem = Job.CreateJob(g, returnedClientJob.JobRef); 

      jobsBindingCollection.Add(newItem); 

      // Add the new item to the context. 
      svcContext.AddToJob(newItem); 

      //// Add the relationship between the order and the new item. 
      //currentOrder.Order_Details.Add(newItem); 
      //svcContext.AddLink(currentOrder, "Order_Details", newItem); 

      //// Set the reference to the order and product from the item. 
      //newItem.Orders = currentOrder; 
      //svcContext.SetLink(newItem, "Orders", currentOrder); 

      // Add the relationship between the product and the new item. 
      returnedClientJob.Job.Add(newItem); 
      svcContext.AddLink(returnedClientJob, "Job", newItem); 

      // Set the reference to the product from the item. 
      newItem.ClientJob = returnedClientJob; 
      svcContext.SetLink(newItem, "ClientJob", returnedClientJob); 
     } 
     ); 
    } 

此代碼是從this使用Northwind數據庫的微軟教程中提取和修改的。由於我的數據庫與Northwind具有相似的結構,因此本教程中的所有其他代碼示例都能正常工作。我已經能夠實施RUD到目前爲止,但不是CRUD。

任何人都可以對這個問題有所瞭解嗎?

幫助非常感謝!

回答

1

您可能需要查看您的實體訪問權限。在您引用的教程的one of the steps上,此配置已配置,但在某些情況下已設置爲「AllRead」。您需要確保您的實體允許寫入權限。

+0

試過一個已經我怕,似乎並沒有讓:-( – Goober 2009-09-09 11:28:39

0

現在有很多層,很難確切地說出問題發生的位置。

當您從Windows應用程序直接向EF模型調用查詢時,它是否工作?如果不是,你的模型中可能有錯誤。

你能從瀏覽器訪問ADO.NET數據服務嗎?

在託管服務的服務器上是否有clientaccesspolicy.xml文件?

您是從http://位置運行silverlight應用程序而不是文件://位置?

+0

差別我取得了一些成功 - 已經改變這一行:URI jobrefUri =新的URI(svcContext.BaseUri.AbsoluteUri +「/ ClientJob( 「+ this.jobref.Text +」)「); to Uri jobrefUri = new Uri(svcContext.BaseUri.AbsoluteUri +」/ ClientJob('「+ this.jobref.Text +」')「);(As你可以看到已經包含單引號來封裝要搜索的值。但是,雖然這有效,但我不完全確定這是正確的實現.... – Goober 2009-09-10 08:37:28