By Allan


2014-05-14 05:54:01 8 Comments

I have read and successfully implemented the solution to support composite keys in OData as outlined in http://code.msdn.microsoft.com/Support-Composite-Key-in-d1d53161
however this issue does not relate to that specifically as the CompositeKeyRoutingConvention class is not even hit.

My issue is that if one of the primary key fields within the model is a datetimeoffset (a supported primitive type), the client generated Uri is not recognised and fails before it is routed.

For example:

GET http://myserver.com:12345/odata/HospitalPens(Pen='C7',StartDate=datetimeoffset'2001-06-30T00:00:00%2B10:00')

This is how the above request is created and executed by the client using a WCF DataService call using the primary key fields.

var start = new DateTimeOffset(2001, 6, 30, 0, 0, 0, new TimeSpan(+10, 0, 0));
var gal = container.HospitalPens.Where(c => c.Pen == "C7" && c.StartDate == start).FirstOrDefault();

the model has the composite keys defined correctly within the Register method in WebApiConfig

var hospitalPens = builder.EntitySet("HospitalPens"); hospitalPens.EntityType.HasKey(e => new { e.Pen, e.StartDate });

If I try to filter on the start date I get the result I'm expecting and all is good. Example call shown below

GET http://myserver.com:12345/odata/HospitalPens?$filter=StartDate%20eq%20datetimeoffset'2001-06-30T00:00:00'

It is not possible to change the database keys so I need a solution to this problem. Thanks in advance.

EDIT: As I see it this is an issue with the way OData clients construct the URL. If the key has any chance of containing an invalid character this will fail due to url validation practices. For example a string key (very common) that contains a ?, even the % and & symbols will cause this to fail. The main issue is that the characters will come before the query string portion of the URL and therefore will cause issues.
But is there a way I can ensure these generated URL's are valid before they are sent to the browser? This makes the odata query structure flawed in my opinion as the odata('key')? url construct won't pass through many request filters.

1 comments

@Tan Jinfu 2014-05-15 00:02:36

Try delete the datetimeoffset prefix and the single quote mark in the url, :

GET http://myserver.com:12345/odata/HospitalPens(Pen='C7',StartDate=2001-06-30T00:00:00%2B10:00).

or, try to escape the ':' in the url:

GET http://myserver.com:12345/odata/HospitalPens(Pen='C7',StartDate=2001-06-30T00%3A00%3A00%2B10%3A00).

Just past it to the address bar of a browser. If any of above works please use this code:

var gal = container.Execute("HospitalPens(Pen='c7',StartDate=xxx)");

@Tan Jinfu 2014-05-15 00:07:51

I'm wondering if you are using the OData Client V4, if no, please follow this blog post: <blogs.msdn.com/b/odatateam/archive/2014/03/11/…>

@Allan 2014-05-15 01:16:49

Thanks, but I don't create the Url so don't know how I can adjust it as suggested. The client side call to container.HospitalPens.Where generates that Url under the covers. I'm wondering if there is a point that I can intercept BEFORE the url is generated? Your suggestion is a work-around to an underlying issue, and whilst it may work, would not be the "correct" answer, as this problem would occur ANY time a date is included in the key and used in a where clause! I also have the luxury at the moment of using the latest versions of DLLs (Microsoft.Data.Odata 5.6.1)

Related Questions

Sponsored Content

3 Answered Questions

[SOLVED] WebApi odata: Serialize long as string

1 Answered Questions

[SOLVED] OData filter options fail when Uri has escaped ampersand

0 Answered Questions

Asp.Net Web Api trailing / causing odata query to fail

3 Answered Questions

[SOLVED] Web API OData (OData Client v4) forward slash in key

1 Answered Questions

[SOLVED] WebApi OData entityset/key/navigation/key support

1 Answered Questions

[SOLVED] ASP.Net WebAPI OData - Composite keys on entities

0 Answered Questions

WebAPI OData DateTime error

0 Answered Questions

WebAPI + OData Service on IIS 5

1 Answered Questions

[SOLVED] ' character in string parameter of Odata Url

1 Answered Questions

Sponsored Content