我可以自定义 .NET linq 提供程序到 oData URI 格式的转换吗?
背景:
Linqpad(或 VS2010 中)
EntityTypes.Where(x => x.EntityUrn == "http://foo")
中的 LINQ 查询转换为以下 oData 兼容 URI:
http://localhost/OData.svc/EntityTypes('http://foo') (这是因为 EntityUrn 是实体键...如果我的 Linq 查询指定任何其他字段,则会使用 $filter 语法)
问题是,由于实体键中的字符受限,此 oData URI 返回错误(请参阅blogs.msdn.com/b/peter_qian/archive/2010/05/25/using-wcf-data-service-with-restricted-charactrers-as-keys.aspx)
那么我想要做的是让这个 LINQ 查询自动转换为等效的 oData 兼容 URI:
http://localhost/OData.svc/ EntityTypes?$filter=EntityUrn eq 'http://foo'
有什么建议吗?
background:
This LINQ query in Linqpad (or in VS2010 for that matter)
EntityTypes.Where(x => x.EntityUrn == "http://foo")
translates to the following oData compliant URI:
http://localhost/OData.svc/EntityTypes('http://foo')
(this is because the EntityUrn is the entity key...if my Linq query specifies any other field, the $filter syntax is used instead)
The problem is that this oData URI returns an error due to restricted characters in the Entity Key (see blogs.msdn.com/b/peter_qian/archive/2010/05/25/using-wcf-data-service-with-restricted-characrters-as-keys.aspx)
So what I want to do is have this LINQ query automatically translate to this equivalent oData compliant URI:
http://localhost/OData.svc/EntityTypes?$filter=EntityUrn eq 'http://foo'
Any suggestions how I can do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如上所述,即使您解决了手头的问题,更大的问题仍然存在,那就是您的实体无法通过其编辑/自我链接进行寻址。这意味着您将无法在查询 (/Customer(0)/Orders) 中使用导航,并且您的服务将无法接受对其实体的任何 PUT/DELETE 请求。
因此,如果可能的话,我会更改模型以不使用 URL 作为关键属性。相反,请使用一些可以作为 URI 路径段一部分的值。
无法直接修改 LINQ 提供程序生成的 URL,主要是因为 URL 生成过程中没有公共挂钩。显然,您可以从头开始编写一个,但这工作量太大。
As already noted above, even if you work around the problem at hand the bigger problem still applies and that is that your entities are not addresable through their edit/self links. This means that you won't able to use navigation in your queries (/Customer(0)/Orders) and that your service won't able to accept any PUT/DELETE requests for its entities.
So if at all possible I would change the model to not use the URL as a key property. Instead use some value which can be part of the URI path segment.
It is not directly possible to modify the URL generated by the LINQ provider, mainly because there's no public hook into the URL generation process. You could obviously write one from scratch, but that's way too much work.