Handling Dates with OData v4, EF6 and Web API v2.2

asp.net-web-api asp.net-web-api2 entity-framework odata

Question

I'm in the process of upgrading from versions 1-3 to version 4, but I've encountered some issues.

According to what I understand, DateTime is not supported, hence I must always use DateTimeOffset. Fine.

However, I was storing SQL earlier.date data type in the DateTime, it appears I'm getting the following 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]'

What can be done to get around this? I require the ability to store only dates in the database (time and locality is not important). Although I didn't have it before, it would be amazing if I could obtain the Edm.Date as well as a returned data type.

Thanks.

Edit: Classes as examples

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; }
}

After:

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; }
}

In EF, this is invalid.

1
7
7/28/2014 8:07:36 AM

Accepted Answer

The OData v4 team has resolved this issue for anyone who comes across it in the future.

[Column(TypeName = "date")]
public DateTime Birthday { get; set; }

This will now resolve to Edm.Date automatically.

If you're doing what I'm doing,date Unless you expressly specify the attributes as dates, type by convention, they will automatically resolve as DateTimeOffset. You cannot presently add your own conventions to OData.

customer.Property(c => c.Birthday).AsDate();

http://odata.github.io/WebApi/#12-01-DateAndTimeOfDayWithEF

4
6/9/2016 8:56:15 AM

Popular Answer

ZZZ_tmp


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow