5

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.

2
  • Why not put a calculated column on the database table to have the datetime? Commented Aug 7, 2015 at 10:05
  • @Mant101 I am not very happy to rummage in the database, because the database belong to a purchased piece of software. The software properly needs the database to be untouched to work correctly. But yes, this will be my last option. I am only doing SELECT operations in the database. Commented Aug 7, 2015 at 10:10

1 Answer 1

2

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); }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I have not access to this.IntDate - I guess IntDate is my actual column name? If I just type this. I can only choose table names and not the attributes from the table.
Which Entity Framework approach do you use? My proposal works when generating entity objects.
I use Code First from Database - And it works! I will manually have to alter all the .cs-files in my model, adding the code-snippet above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.