Handling Dates with OData v4, EF6 and Web API v2.2

前端 未结 4 420
孤城傲影
孤城傲影 2020-12-10 16:14

I\'m in the midst of upgrading from v1-3 to v4, but I\'ve run into a few problems.

My understanding is that DateTime is unsupported, and I have to always use DateTim

4条回答
  •  北海茫月
    2020-12-10 16:42

    You can refer to the link below to define your DateTimeAndDateTimeOffsetWrapper to do the translation between two types.

    http://www.odata.org/blog/how-to-use-sql-spatial-data-with-wcf-odata-spatial/

    Define two properties on your model, one is DateTime which only exists in the Edm model, the other is DateTimeOffset which only exists in the DB.


    If the solution above doesn't meet your request, you have to change the data to DateTime before saving it to database and change it back to DateTimeOffset after retrieving it from database in the controller actions.

    You can define two almost-same classes to achieve this. The only difference is that one has DateTime property and the other has DateTimeOffset property.

    The former one is used for EF and mapping into DB.

    The latter one is used for defining OData Edm model and presenting to the users.

    As I said above, you have to do the translation between these two classes before saving the data and after retrieving the data.

提交回复
热议问题