【问题标题】:The type System.DateTime is not a supported type. Change to use System.DateTimeOffsetSystem.DateTime 类型不是受支持的类型。更改为使用 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.Nullable1[[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
内部异常:

【问题讨论】:

标签: mysql asp.net-web-api odata entity-framework-6 dotconnect


【解决方案1】:

我相信,从最新版本的 WebApi OData v4 (5.4.0) 开始,DateTime 得到了一定程度的支持。它仍然在元数据中定义为 DateTimeOffset,但至少您的模型可以处理 DateTime。详情请见http://odata.github.io/WebApi/datetime-support/

【讨论】:

    猜你喜欢
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-31
    • 1970-01-01
    • 2014-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多