2013-07-05 20 views
0

我在通過ADO.Net實體模型連接到Oracle數據庫的控制檯應用程序中自主託管Odata WCF數據服務。我正在使用Microsoft.Data.Services v5.5.0.I可以通過客戶端應用程序讀取/寫入數據,但無法刪除。下面是代碼:通過WFC數據服務客戶端應用程序刪除數據

Server代碼

public class tos_host : DataService<Entities> 
{ 
    public static void InitializeService(DataServiceConfiguration config) 
    { 
     // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc. 
     // Examples: 
     config.SetEntitySetAccessRule("*", EntitySetRights.All); 
     config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3; 
    } 
} 
class Program 
{ 
    static void Main(string[] args) 
    { 
     Uri[] baseAddresses = new Uri[1]; 
     baseAddresses[0] = new Uri("http://localhost:4444/tos_host"); 
     using (DataServiceHost host = new DataServiceHost(typeof(tos_host), baseAddresses)) 
     { 
      host.Open(); 
      Console.WriteLine("TOS host up and running....."); 
      Console.ReadLine(); 
      host.Close(); 
     } 
    } 
}  

客戶端代碼

static void Main(string[] args) 
    { 
     Entities context = new Entities(new Uri("http://localhost:4444/tos_host")); 
     context.MergeOption = MergeOption.OverwriteChanges; 
     context.IgnoreMissingProperties = true; 

     var container = new CONTAINERS(); 
     container.CONTAINER_ID = "CONT"; 
     container.TIMESTAMP=DateTime.UtcNow; 
     context.AddToCONTAINERS(container); 
     context.SaveChanges(); 
     CONTAINERS container_2 = context.CONTAINERS.Where(c => c.CONTAINER_ID == "CONT").First(); 
     context.DeleteObject(container_2); 
     context.SaveChanges();   //Here i get an exception 
    } 

例外如下:

System.Data.Services.Client.DataServiceRequestException was unhandled 
HResult=-2146233079 
Message=An error occurred while processing this request. 
Source=Microsoft.Data.Services.Client 
StackTrace: 
    at System.Data.Services.Client.SaveResult.HandleResponse() 
    at System.Data.Services.Client.BaseSaveResult.EndRequest() 
    at System.Data.Services.Client.DataServiceContext.SaveChanges(SaveChangesOptions options) 
    at System.Data.Services.Client.DataServiceContext.SaveChanges() 
    at client_test_lnew_libs.Program.Main(String[] args) in c:\Users\ITS\Desktop\rmcs_tests  \client_test_lnew_libs\client_test_lnew_libs\Program.cs:line 27 
    at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
    at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
    at System.Threading.ThreadHelper.ThreadStart() 
    InnerException: System.Data.Services.Client.DataServiceClientException 
    HResult=-2146233079 
    Message=<?xml version="1.0" encoding="utf-8"?><m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"><m:code /><m:message xml:lang="el-GR">Resource not found for the segment 'CONTAINERS'.</m:message></m:error> 
    StatusCode=404 
    InnerException: 

我在做什麼錯誤?任何線索?

回答

0

根據我的理解,當您查詢並且沒有返回結果時,dataservice會拋出您所看到的異常。返回一個空集,而不是,你需要設置IgnoreResourceNotFoundException屬性:

context.IgnoreResourceNotFoundException = true; 

請參閱以下博客文章的詳細信息:

http://blogs.msdn.com/b/peter_qian/archive/2009/03/20/safe-resource-not-found-404-exception-handling-in-ado-net-data-service-client.aspx?Redirected=true

+0

謝謝ChrisO。我的問題是,查詢確實返回一個結果,它被標記爲刪除,然後出現異常,奇怪! – Td84

+0

@ Td84你是如何解決它結束的? 我有完全相同的錯誤,我沒有得到快速的地方! – EdsonF

相關問題