System.DateTime 类型不是受支持的类型。更改为使用 System.DateTimeOffset
Posted
技术标签:
【中文标题】System.DateTime 类型不是受支持的类型。更改为使用 System.DateTimeOffset【英文标题】:The type System.DateTime is not a supported type. Change to use System.DateTimeOffset 【发布时间】:2014-09-30 23:55:21 【问题描述】:我们正在创建 WebApi 2.2 服务并使用上面列出的技术。我们的后端数据存储是 mysql 5.6。我们正在使用 dotConnect for MySql 来处理数据存储。在数据库中,有一个类型为 Timestamp 的 RowVersion 列。在 EF 中,我成功生成了模型,但我注意到 RowVersion 设置为 DateTime。当我运行 WebApi 时,出现以下运行时异常,因此我需要将类型更改为 DateTimeOffset,因为 Timestamp 不可用。
在我们的应用程序中,我们将使用带有 ETag 的 RowVersion 进行并发处理。因此,我们只会在应用程序中读取 RowVersion;每当插入或更新发生时,数据库将自动更新 RowVersion。
我不知道如何纠正这个问题...也许,有一些方法可以添加自动类型转换,因此模型中的 RowVersion 是 Int64,我们通过发送 Timestamp 自动在 Int64 和 Timestamp 之间转换。对我们的应用程序的价值。我们只是在阅读它,所以这似乎是合理的。
当我在 EF 模型中将 RowVersion 更改为 Int64 并构建应用程序时,我收到以下错误:
错误 1 错误 2019:指定的成员映射无效。方式 成员“版本”的“Edm.Int64[Nullable=True,DefaultValue=]”类型 'Model.customer' 不兼容 'Devart.Data.MySql.timestamp[Nullable=True,DefaultValue=,Precision=0]' 类型中的成员“版本” '模型.商店.客户'。 C:\PROJECTS\ServiceMySql\ServiceMySql\Models\Model.edmx 898 17 ServiceMySql
非常感谢您帮助找出解决此问题的方法。
感谢您的宝贵时间和建议,
迈克
本文开头提到的异常:
System.ArgumentException 未被用户代码处理 HResult=-2147024809 Message=类型 'System.Nullable
1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' of property 'Version' in the 'XXXXServiceMySql.Models.customer' type is not a supported type. Change to use 'System.DateTimeOffset' or ignore this type by calling Ignore<XXXXServiceMySql.Models.customer>() on 'System.Web.OData.Builder.ODataModelBuilder'. Parameter name: navigationProperty Source=System.Web.OData ParamName=navigationProperty StackTrace: at System.Web.OData.Builder.EntityTypeConfiguration.AddNavigationProperty(PropertyInfo navigationProperty, EdmMultiplicity multiplicity, Boolean containsTarget) at System.Web.OData.Builder.EntityTypeConfiguration.AddNavigationProperty(PropertyInfo navigationProperty, EdmMultiplicity multiplicity) at System.Web.OData.Builder.ODataConventionModelBuilder.MapEntityType(EntityTypeConfiguration entity) at System.Web.OData.Builder.ODataConventionModelBuilder.MapType(StructuralTypeConfiguration edmType) at System.Web.OData.Builder.ODataConventionModelBuilder.MapTypes() at System.Web.OData.Builder.ODataConventionModelBuilder.GetEdmModel() at XXXXServiceMySql.WebApiConfig.GenerateEdmModel() in c:\PROJECTS\XXXXServiceMySql\XXXXServiceMySql\App_Start\WebApiConfig.cs:line 89 at XXXXServiceMySql.WebApiConfig.Register(HttpConfiguration config) in c:\PROJECTS\XXXXServiceMySql\XXXXServiceMySql\App_Start\WebApiConfig.cs:line 55 at System.Web.Http.GlobalConfiguration.Configure(Action
1 配置回调) 在 XXXXServiceMySql.WebApiApplication.Application_Start() 在 c:\PROJECTS\XXXXServiceMySql\XXXXServiceMySql\Global.asax.cs:line 17 内部异常:
【问题讨论】:
我删除了标题中的标签,Should questions include tags in their titles? 目前web api odata 不支持DateTime,也许会有修复。有一个类似的问题可以解决,希望这会有所帮助***.com/questions/24829422/…> 某些喜剧演员似乎更改了 OData v4 服务,以破解 99% 的已编写并处理日期时间信息的所有代码,选择:aspnetwebstack.codeplex.com/workitem/1753。 投票给aspnetwebstack.codeplex.com/workitem/2072和aspnet.uservoice.com/forums/147201-asp-net-web-api/suggestions/… 另一个类似的线程 ***.com/questions/25189557/… 。希望这可以暂时帮助您解决问题 【参考方案1】:我相信,从最新版本的 WebApi OData v4 (5.4.0) 开始,DateTime 得到了一定程度的支持。它仍然在元数据中定义为 DateTimeOffset,但至少您的模型可以处理 DateTime。详情请见http://odata.github.io/WebApi/datetime-support/。
【讨论】:
以上是关于System.DateTime 类型不是受支持的类型。更改为使用 System.DateTimeOffset的主要内容,如果未能解决你的问题,请参考以下文章
无法将类型为“System.DateTime”的对象强制转换为类型“System.String”
拒绝应用样式,因为它的 MIME 类型 ('text/html') 不是受支持的样式表 MIME 类型
为啥 System.DateTime 结构的布局类型为 Auto?
节点拒绝应用样式,因为它的 MIME 类型('text/html')不是受支持的样式表 MIME
CSS/JS 在 HTML 中不起作用,错误:MIME 类型 ('text/html') 不是受支持的样式表 MIME 类型