我在控制台应用程序中自行托管Odata WCF数据服务,该应用程序通过ADO.Net实体模型连接到Oracle数据库。我正在使用Microsoft.Data.Services v5.5.0。我可以通过客户端应用程序读/写数据,但无法删除。这是代码:
服务器代码:
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 :(得分:0)
根据我的理解,当您查询并且没有返回任何结果时,dataservice将抛出您看到的异常。要改为返回空集,需要设置IgnoreResourceNotFoundException属性:
context.IgnoreResourceNotFoundException = true;
有关详细信息,请参阅以下博文: