15

当使用 ADO.Net 数据服务客户端通过调用刷新实体时LoadProperty

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

如果属性是,它会在服务器上引发错误null

错误:抛出异常:System.Data.Services.DataServiceException:找不到段“所有者”的资源。在 System.Data.Services.RequestDescription.GetSingleResultFromEnumerable(SegmentInfo segmentInfo) 在 System.Data.Services.DataService1.CompareETagAndWriteResponse(RequestDescription description, ContentFormat responseFormat, IDataService dataService)
在 System.Data.Services.DataService1.SerializeResponseBody(RequestDescription description, IDataService dataService ) 在 System.Data.Services.DataService1.HandleNonBatchRequest(RequestDescription description) 在 System.Data.Services.DataService`1.HandleRequest()

问题是客户不知道该属性是否null已填充或尚未填充。属性 Owner 是从 aVehicle到 a的链接Customer

有什么想法有什么问题吗?

谢谢

4

3 回答 3

33

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

svc.IgnoreResourceNotFoundException = true;
于 2011-05-13T05:50:18.040 回答
20

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

如果没有虚拟表达式,ADO.NET 请求是:

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

在虚拟条件下,请求是:

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

在第二种情况下,预期的行为(返回 null)是正确的。

于 2009-12-06T15:24:43.170 回答
3

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

于 2009-02-10T04:36:43.847 回答