gpt4 book ai didi

.net - 如何让 Web API OData v4 使用 DateTime

转载 作者:行者123 更新时间:2023-12-03 10:13:36 28 4
gpt4 key购买 nike

我有一个相当大的数据模型,我想使用 OData V4 协议(protocol)使用 Web API OData 公开它。

基础数据存储在 SQL Server 2012 数据库中。该数据库中有许多 DateTime 列。

当我连接它时,我收到一个错误,即 System.DateTime 不受支持。

所以这是我的问题,我该怎么做才能在 OData 提要中看到我的 DateTime 列?

注意:我无法返回并将所有列更改为 DateTimeOffset 列。

我尝试更改 Entity Framework edmx 中列的类型,但它给了我这个错误:

Member Mapping specified is not valid. The type 'Edm.DateTimeOffset[Nullable=False,DefaultValue=,Precision=]' of member 'MyPropertyHere' in type 'MyProject.MyEntity' is not compatible with 'SqlServer.datetime[Nullable=False,DefaultValue=,Precision=3]' of member 'MyColumnName' in type 'MyDataModel.Store.MyEntity'.



(基本上认为 DateTime 与 DateTimeOffset 不兼容。)

Web API OData 团队真的忽略了所有需要使用 DateTime 的 SQL Server 类型的人吗? ?

更新:我找到了解决方法,但它们需要更新 EF 模型才能工作。如果可以避免的话,我宁愿不必单独更新数百个属性。

Update: This issue has made me realize that there are deep flaws in how Microsoft is managing its OData products. There are many issues, but this one is the most glaring. There are huge missing features in the Web API OData. Transactions and ordering of inserts being two of them. These two items (that are in the OData spec and were in WCF Data Services before Microsoft killed it) are critical to any real system.

But rather than put time into those critical spots where they are missing functionality that is in the OData Specification, they decide to spend their time on removing functionality that was very helpful to many developers. It epitomizes bad management to prioritize the removal of working features over adding in badly needed features.

I tried discussing these with the Web API OData representative, and in the end, I got a issue/ticket opened that was then closed a few days later. That was the end of what they were willing to do.

As I said, there are many more issues (not related to DateTime, so I will not list them here) with the management of Web API OData. I have been a very strong supporter of OData, but the glaring issues with Web API OData's management have forced me and my team/company to abandon it.

Fortunately, normal Web API can be setup to use OData syntax. It is more work to setup your controllers, but it works just fine in the end. And it supports DateTime. (And seems to have management that can at least stay away from making insanely bad decisions.)

最佳答案

最后 Web API OData v4 现在支持 DateTime输入 release 5.5 。获取最新的 nuget 包,不要忘记设置:

config.SetTimeZoneInfo(TimeZoneInfo.Utc);

否则 dateTime 属性的时区将被视为本地时区。

更多信息: ASP.NET Web API for OData V4 Docs DateTime support

关于.net - 如何让 Web API OData v4 使用 DateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25189557/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com