I have a fairly large data model that I want to expose using Web API OData using the OData V4 protocol.
The underlying data is stored in a SQL Server 2012 database.
public class Customer
{
private DateTimeWrapper dtw;
public int Id { get; set; }
public string Name { get; set; }
public DateTime Birthday
{
get { return dtw; }
set { dtw = value; }
}
[NotMapped]
public DateTimeOffset BirthdayOffset
{
get { return dtw; }
set { dtw = value; }
}
}
var customer = builder.EntityType<Customer>();
customer.Ignore(t => t.Birthday);
customer.Property(t => t.BirthdayOffset).Name = "Birthday";
Since i use a library for odata with angular, i investigated it there:
https://github.com/devnixs/ODataAngularResources/blob/master/src/odatavalue.js
There you can see ( javascript)
var generateDate = function(date,isOdataV4){
if(!isOdataV4){
return "datetime'" + date.getFullYear() + "-" + ("0" + (date.getMonth() + 1)).slice(-2) + "-" + ("0" + date.getDate()).slice(-2) + "T" + ("0" + date.getHours()).slice(-2) + ":" + ("0" + date.getMinutes()).slice(-2)+':'+("0" + date.getSeconds()).slice(-2) + "'";
}else{
return date.getFullYear() + "-" + ("0" + (date.getMonth() + 1)).slice(-2) + "-" + ("0" + date.getDate()).slice(-2) + "T" + ("0" + date.getHours()).slice(-2) + ":" + ("0" + date.getMinutes()).slice(-2)+':'+("0" + date.getSeconds()).slice(-2) + "Z";
}
};
And the test expects ( cfr. here )
$httpBackend.expectGET("/user(1)?$filter=date eq 2015-07-28T10:23:00Z")
So this should be your "formatting"
2015-07-28T10:23:00Z
An alternate solution is to patch the project so that DateTime
is allowed again - that's what I did, you can get the code here if you want it:
https://aspnetwebstack.codeplex.com/SourceControl/network/forks/johncrim/datetimefixes
I also pushed a NuGet package to make it easy to use, you can obtain the package using the info here:
http://www.nuget.org/packages/Patches.System.Web.OData/5.3.0-datetimefixes
... I don't know if it's ok for me to post a NuGet package containing a patched Microsoft library, I hope it's easier to ask forgiveness than permission.
Note that the work item to officially restore the ability to use DateTime in OData 4 is tracked here: https://aspnetwebstack.codeplex.com/workitem/2072 Please vote for the issue if you'd like to see it; though it looks like it's scheduled for 5.4-beta, so it should be coming one of these days.
Finally Web API OData v4 now supports DateTime
type in release 5.5 . Get latest nuget package and don't forget setting this:
config.SetTimeZoneInfo(TimeZoneInfo.Utc);
otherwise the timezone of the dateTime property would be considered as local timezone.
More info: ASP.NET Web API for OData V4 Docs DateTime support
Looks like a mapping DateTimeOffset <-> DateTime will be included in Microsoft ASP.NET Web API 2.2 for OData v4.0 5.4.0:
https://github.com/OData/WebApi/commit/2717aec772fa2f69a2011e841ffdd385823ae822
Install System.Web.OData 5.3.0-datetimefixes from https://www.nuget.org/packages/Patches.System.Web.OData/