0

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 Answer 1

0

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)");
2
  • 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/…>
    – Tan Jinfu
    May 15, 2014 at 0:07
  • 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)
    – Allan
    May 15, 2014 at 1:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.