本文转 제작: http://chris.eldredge.io/blog/2014/04/24/Composite-Keys/
기본 구성에서 우리는 엔터티에 ID와 버전으로 구성된 복합 키가 있다고 모델 빌더에게 알렸습니다.
으아악 | 으아악 |
이것은 OData 피드가 다음과 같은 형식으로 각 개별 엔터티에 대한 편집
및 자체
링크를 렌더링하는 데 충분합니다.edit
and self
links for each individual entity in a form like:
<span class="line-number">1 <span class="line-number">2 <span class="line-number">3 <span class="line-number">4 <span class="line-number">5 <span class="line-number">6 <span class="line-number">7 <span class="line-number">8 <span class="line-number">9 <span class="line-number">10 </span></span></span></span></span></span></span></span></span></span>
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:
<code class="c#"><span class="line"><span class="k">public <span class="k">void <span class="nf">MapDataServiceRoutes<span class="p">(<span class="n">HttpConfiguration <span class="n">config<span class="p">) <span class="line"><span class="p">{ <span class="line"> <span class="kt">var <span class="n">builder <span class="p">= <span class="k">new <span class="n">ODataConventionModelBuilder<span class="p">(); <span class="line"> <span class="line"> <span class="kt">var <span class="n">entity <span class="p">= <span class="n">builder<span class="p">.<span class="n">EntitySet<span class="p">7879bc2b6ec581464773019d50856563(<span class="s">"Packages"<span class="p">); <span class="line"> <span class="n">entity<span class="p">.<span class="n">EntityType<span class="p">.<span class="n">HasKey<span class="p">(<span class="n">pkg <span class="p">=> <span class="n">pkg<span class="p">.<span class="n">Id<span class="p">); <span class="line"> <span class="n">entity<span class="p">.<span class="n">EntityType<span class="p">.<span class="n">HasKey<span class="p">(<span class="n">pkg <span class="p">=> <span class="n">pkg<span class="p">.<span class="n">Version<span class="p">); <span class="line"> <span class="line"> <span class="c1">// snip <span class="line"><span class="p">} </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code> |
<code>http://localhost/odata/Packages(Id='Sample',Version='1.0.0') </code> |
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:
<span class="line-number">1 <span class="line-number">2 <span class="line-number">3 <span class="line-number">4 <span class="line-number">5 <span class="line-number">6 <span class="line-number">7 <span class="line-number">8 <span class="line-number">9 <span class="line-number">10 <span class="line-number">11 <span class="line-number">12 <span class="line-number">13 <span class="line-number">14 <span class="line-number">15 <span class="line-number">16 <span class="line-number">17 <span class="line-number">18 <span class="line-number">19 <span class="line-number">20 </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span> |
<code class="c#"><span class="line"><span class="k">public <span class="k">class <span class="nc">PackagesODataController <span class="p">: <span class="n">ODataController <span class="line"><span class="p">{ <span class="line"> <span class="k">public <span class="n">IMirroringPackageRepository <span class="n">Repository <span class="p">{ <span class="k">get<span class="p">; <span class="k">set<span class="p">; <span class="p">} <span class="line"> <span class="line"> <span class="k">public <span class="n">IQueryable<span class="p">7879bc2b6ec581464773019d50856563 <span class="n">Get<span class="p">() <span class="line"> <span class="p">{ <span class="line"> <span class="k">return <span class="n">Repository<span class="p">.<span class="n">GetPackages<span class="p">().<span class="n">Select<span class="p">(<span class="n">p <span class="p">=> <span class="n">p<span class="p">.<span class="n">ToODataPackage<span class="p">()).<span class="n">AsQueryable<span class="p">(); <span class="line"> <span class="p">} <span class="line"> <span class="line"> <span class="k">public <span class="n">IHttpActionResult <span class="nf">Get<span class="p">( <span class="line"><span class="na"> [FromODataUri] <span class="kt">string <span class="n">id<span class="p">, <span class="line"><span class="na"> [FromODataUri] <span class="kt">string <span class="n">version<span class="p">) <span class="line"> <span class="p">{ <span class="line"> <span class="kt">var <span class="n">package <span class="p">= <span class="n">Repository<span class="p">.<span class="n">FindPackage<span class="p">(<span class="n">id<span class="p">, <span class="n">version<span class="p">); <span class="line"> <span class="line"> <span class="k">return <span class="n">package <span class="p">== <span class="k">null <span class="line"> <span class="p">? <span class="p">(<span class="n">IHttpActionResult<span class="p">)<span class="n">NotFound<span class="p">() <span class="line"> <span class="p">: <span class="n">Ok<span class="p">(<span class="n">package<span class="p">.<span class="n">ToODataPackage<span class="p">()); <span class="line"> <span class="p">} <span class="line"><span class="p">} </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code> |
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
으아악
올바른 동작을 얻으려면 먼저 키로 개별 엔터티 인스턴스를 가져오는 PackagesODataController를 재정의해야 합니다.
🎜🎜🎜 🎜 으아악 🎜 🎜 으아악 🎜 🎜🎜🎜 🎜 🎜이 클래스는 표준
EntityRoutingConvention
을 장식하고 URI의 원시 키 부분을 키/값 쌍으로 분할하고 이를 모두 RouteValues 사전에 추가합니다.🎜
🎜이 작업이 완료되면 표준 작업 해결이 시작되어 호출할 올바른 작업 오버로드를 찾습니다.🎜
🎜이 라우팅 규칙은 WebApi ODataCompositeKeySample프로젝트에서 채택되었습니다.🎜
🎜여기서 WebApi OData와 WCF 데이터 서비스의 또 다른 차이점을 볼 수 있습니다. WCF 데이터 서비스에서 프레임워크는 IQueryable
에서 단일 인스턴스를 선택하는 쿼리 생성을 처리합니다. 이로 인해 키로 인스턴스를 찾는 방법을 사용자 정의하는 기능이 제한됩니다. WebApi OData에서는 키로 엔터티 인스턴스를 가져오는 오버로드를 명시적으로 정의해야 쿼리 실행 방법을 더 효과적으로 제어할 수 있습니다.🎜
🎜이 구별은 대부분의 프로젝트에서 중요하지 않을 수 있지만 NuGet.Lucene.Web의 경우 로컬 피드가 다른 서버에서 즉시 패키지를 가져와 로컬 피드에 추가할 수 있는 주문형 미러링 기능을 활성화합니다. 그런 다음 처음에 항상 있었던 것처럼 클라이언트에 다시 보냅니다.🎜
🎜WCF 데이터 서비스에서 이를 사용자 정의하려면 상당한 반전이 필요했습니다.🎜
🎜 🎜
🎜시리즈 색인🎜
🎜
🎜소개🎜
🎜기본 WebApi OData🎜
🎜복합 키🎜
🎜기본 스트림🎜
🎜
🎜 🎜위 내용은 [转]WebApi OData를 사용한 복합 키의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!