[转]Composite Keys With WebApi OData
本文转自:http://chris.eldredge.io/blog/2014/04/24/Composite-Keys/
In our basic configuration we told the model builder that our entity has a composite key comprised of an ID and a version:
1 |
|
This is enough for our OData feed to render edit
and self
links for each individual entity in a form like:
http://localhost/odata/Packages(Id='Sample',Version='1.0.0')
But if we navigate to this URL, instead of getting just this one entity by key, we get back the entire entity set.
To get the correct behavior, first we need an override on our PackagesODataController that gets an individual entity instance by key:
1 |
|
However, out of the box WebApi OData doesn’t know how to bind composite key parameters to an action such as this, since the key is comprised of multiple values.
We can fix this by creating a new routing convention that binds the stuff inside the parenthesis to our route data map:
1 |
|
This class decorates a standard EntityRoutingConvention
and splits the raw key portion of the URI into key/value pairs and adds them all to the routeValues dictionary.
Once this is done the standard action resolution kicks in and finds the correct action overload to invoke.
This routing convention was adapted from the WebApi ODataCompositeKeySampleproject.
Here we see another difference between WebApi OData and WCF Data Services. In WCF Data Services, the framework handles generating a query that selects a single instance from an IQueryable
. This limits our ability to customize how finding an instance by key is done. In WebApi OData, we have to explicitly define an overload that gets an entity instance by key, giving us more control over how the query is executed.
This distinction might not matter for most projects, but in the case of NuGet.Lucene.Web, it enables a mirror-on-demand capability where a local feed can fetch a package from another server on the fly, add it to the local repository, then send it back to the client as if it was always there in the first place.
To customize this in WCF Data Services required significant back flips.
Series Index
最新文章
- Pointcut is malformed: Pointcut is not well-formed: expecting 'identifier' at character position 0 ^
- HTML 代码复用实践 (静态页面公共部分提取复用)
- Atitit 拦截数据库异常的处理最佳实践
- java中如何生成可执行的jar文件
- JQuery获取元素的方法总结
- sqoop的增量导入(increment import)
- centOS安装openoffice
- MySQL 字段类型详解
- IOS 本地通知
- 此文件时入口文件index.php
- C#生成随机汉字
- eclipse创建一个文件夹
- GUI—ST_emWin移植
- Redis(转)
- 平台加载面板的方法$.loadPml
- Entity Framework Core的贴心:优雅处理带默认值的数据库字段
- VB播放声音
- 设置获取cookie,setCookie,getCookie
- js文件,同样的路径,拷贝过来的为什么不能访问
- EF5+MVC4系列(1) Podwerdesigner15.1设计数据库;PD中间表和EF实体模型设计器生成中间表的区别;EF5.0 表关系插入数据(一对多,多对多)