处理与OData的V4日期,EF6和Web API V2.2 [英] Handling Dates with OData v4, EF6 and Web API v2.2
问题描述
我在从V1-3升级到V4之中,但我已经遇到了一些问题。
I'm in the midst of upgrading from v1-3 to v4, but I've run into a few problems.
我的理解是,日期时间是不支持的,我也总是使用的DateTimeOffset。精细。
My understanding is that DateTime is unsupported, and I have to always use DateTimeOffset. Fine.
但我存储SQL的日期时间日期
数据类型之前,现在看来,我得到这个错误:
But before I was storing Sql date
data type in the DateTime, now it seems I get this error:
Member Mapping specified is not valid. The type 'Edm.DateTimeOffset[Nullable=False,DefaultValue=,Precision=]' of member 'CreatedDate' in type 'MyEntity' is not compatible with 'SqlServer.date[Nullable=False,DefaultValue=,Precision=0]'
什么是工作都是围绕这个?我需要能够特异性地只存储日期在数据库中(时间和位置并不重要)。将是巨大的,如果我能得到Edm.Date藏汉为返回的数据类型,但我没有过。
What is the work around for this? I need to be able to store specifically just dates in the database (time and locality is not important). Would be great if I could get the Edm.Date aswell as a returned data type, but I didn't have that before.
感谢。
编辑:示例类
在
public class Ticket
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required, MaxLength(50)]
public string Reference { get; set; }
[Column(TypeName = "date")]
public DateTime LoggedDate { get; set; }
}
在
public class Ticket
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required, MaxLength(50)]
public string Reference { get; set; }
[Column(TypeName = "date")]
public DateTimeOffset LoggedDate { get; set; }
}
这是无效的EF
推荐答案
有关的人来这在未来,OData的V4团队已经修复了这个问题。
For anyone coming to this in the future, the OData v4 team have fixed this issue.
[Column(TypeName = "date")]
public DateTime Birthday { get; set; }
这将立即自动解析到Edm.Date。
This will now auto-resolve to Edm.Date.
如果你和我一样,都在做日期
键入按照惯例,你必须为日期,以免他们被自动解析为的DateTimeOffset手工申报的属性。目前OData的不允许添加自己的约定。
If you are like me and are doing date
type by convention, you have to manually declare the properties as dates lest they be auto-resolved as DateTimeOffset. OData currently does not allow you to add your own conventions.
customer.Property(c => c.Birthday).AsDate();
http://odata.github.io/WebApi/#12-01- DateAndTimeOfDayWithEF
这篇关于处理与OData的V4日期,EF6和Web API V2.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!