How to get Web API OData v4 to use DateTime

前端 未结 12 1181
时光取名叫无心
时光取名叫无心 2020-12-02 14:50

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.

相关标签:
12条回答
  • 2020-12-02 15:03
    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";
    
    0 讨论(0)
  • 2020-12-02 15:08

    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
    
    0 讨论(0)
  • 2020-12-02 15:10

    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.

    0 讨论(0)
  • 2020-12-02 15:12

    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

    0 讨论(0)
  • 2020-12-02 15:13

    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

    0 讨论(0)
  • 2020-12-02 15:13

    Install System.Web.OData 5.3.0-datetimefixes from https://www.nuget.org/packages/Patches.System.Web.OData/

    0 讨论(0)
提交回复
热议问题