2009-02-03 99 views
16

当使用ADO.Net数据服务客户端通过调用刷新的实体LoadProperty资源未找到部分“财产”

ctx.BeginLoadProperty(this, "Owner", (IAsyncResult ar) => ... 

如果属性是null

它抛出在服务器上的错误

Error: Exception Thrown: System.Data.Services.DataServiceException: Resource not found for the segment 'Owner'. at System.Data.Services.RequestDescription.GetSingleResultFromEnumerable(SegmentInfo segmentInfo) at System.Data.Services.DataService1.CompareETagAndWriteResponse(RequestDescription description, ContentFormat responseFormat, IDataService dataService)
at System.Data.Services.DataService1.SerializeResponseBody(RequestDescription description, IDataService dataService) at System.Data.Services.DataService1.HandleNonBatchRequest(RequestDescription description) at System.Data.Services.DataService`1.HandleRequest()

问题是客户端不知道该属性是否为null或者尚未填充。业主是从VehicleCustomer的链接。

任何想法有什么不对?

谢谢

回答

19

当主键不存在时,在主键上查询会产生异常。解决方法是在条件中添加虚拟真实表达式(例如:1 == 1 & & item.Id == XXX)。

没有虚设表达的ADO.NET请求是:

http: //localhost//test.svc/Role(XXX)

随着虚设状态下,该请求是:

http: //localhost//test.svc/Role()?$filter=true and (Id eq 1)

预期的行为(空返回)是正确的在第二案件。

+0

这[其他答案](http://stackoverflow.com/questions/505761/resource-not-found-for-segment-property/5987733#5987733)是一个更好的选择。提高代码的可读性。 – 2011-11-15 03:32:44

3

我收到了“资源找不到段'属性'”错误也。我的主要关键在于我在查找的where子句中。我发现一些资源表示在使用不存在的主键(而不是其他where子句不提供结果)时它会抛出错误(即使在使用FirstOrDefault()时)。我猜想类似的事情正在发生在你自己身上。

+0

似乎是这样,我不知道为什么? – gimpy 2009-05-20 08:09:12

34

集服务上下文的IgnoreResourceNotFoundException属性设置为true:

svc.IgnoreResourceNotFoundException = true; 
+0

请注意,此属性是在.NET 3.5 SP1中添加的。 – 2011-11-15 03:33:23