System.DateTime类型不是受支持的类型。更改为使用System.DateTimeOffset

时间:2014-08-07 16:28:18

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

我们正在创建WebApi 2.2服务,并且正在使用上面列出的技术。我们的后端数据存储区是MySql 5.6。我们使用dotConnect for MySql来处理数据存储。在数据库中,有一个RowVersion列,其类型为Timestamp。在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 =]'成员'Version'的类型   'Model.customer'与之不兼容   'Devart.Data.MySql.timestamp [可空=真,默认值=,精密= 0]'   成员'版本'的类型   'Model.Store.customers'。 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 configurationCallback)          位于c:\ PROJECTS \ XXXXServiceMySql \ XXXXServiceMySql \ Global.asax.cs中的XXXXServiceMySql.WebApiApplication.Application_Start():第17行
  的InnerException:

1 个答案:

答案 0 :(得分:1)

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