C# 未找到段“属性”的资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/505761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 06:02:39  来源:igfitidea点击:

Resource not found for segment 'Property'

c#wcf-data-services

提问by gimpy

When using ADO.Net Data Services client to refresh an entity by calling the LoadProperty:

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

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

It throws an error on the server if the property is null

如果属性是,它会在服务器上引发错误 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()

错误:抛出异常: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()

Problem is that the client does not know whether the property is nullor just hasn't been populated yet. The property Owner is a link from a Vehicleto a Customer.

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

Any ideas what's wrong?

任何想法有什么问题?

Thanks

谢谢

采纳答案by Francois YACOB

Querying on primary keys generate an exception when the key does not exist. The workaround is to add a dummy true expression in the condition (eg : 1==1 && item.Id == XXX).

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

Without the dummy expression the ADO.NET request is:

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

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

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

With the dummy condition, the request is:

在虚拟条件下,请求是:

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

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

The expected behaviour (null returned) is correct in the second case.

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

回答by ChrisHDog

I've received a "Resource not found for segment 'Property'" error also. Mine appears to be that in the where clause I am looking by primary key. I have found some resources that say it will throw an error (even when using FirstOrDefault()) when using a primary key that doesn't exist (as opposed to other where clauses that just do not provide results). I'd guess a similar thing is happening to yourself.

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

回答by Pavel Chuchuva

Set IgnoreResourceNotFoundExceptionproperty of the service context to true:

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

svc.IgnoreResourceNotFoundException = true;