Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert int timestamp to DateTime using Entity Framework

I have a database where the columns are int datatypes. The column int-values corresponds to seconds since 1980-01-01 (MS-DOS timestamp).

Is it possible to convert these int-values using Entity Framework? If not that else to do? The conversion needs to be efficient.

I have tried the following SQL in my controller but I receive a JSON error from the client (Fiddler output: JSON=-1):

public JsonResult GetData()
{
     var model = entities.Database.ExecuteSqlCommand(
        @"SELECT TOP 10 
                 dateadd(S, [timestamp], '1980-01-01') [datetime_conv], 
                 [value] 
            FROM [Data_2696415_20] AS tabel");

     return Json(model, JsonRequestBehavior.AllowGet);
 }

I am using Entity Framework version 6.1.

like image 390
VBMan Avatar asked Oct 31 '22 22:10

VBMan


1 Answers

I would put an additional property on the entity class which gets the original int-value through a converter:

public partial class MyEntity
{
    public DateTime Date
    {
        get { return ConvertToDateTime(this.IntDate); }
    }
}
like image 179
Patrick Avatar answered Nov 15 '22 05:11

Patrick