This is a migrated thread and some comments may be shown as answers.

Mapping SQL datetime field to DateTimeOffset property

2 Answers 437 Views
Databases and Data Types
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Cory
Top achievements
Rank 1
Cory asked on 16 Oct 2015, 08:24 PM

Hi,

 I'm in the process of creating OData services for a legacy SQL Server database which uses datetime fields. The issue is that OData doesn't support DateTime properties, and I can't figure out how to map a datetime field to a DateTimeOffset property using fluent mapping so that OData will work.

 I've tried:

 

configuration.HasProperty(x => x.CreatedOn).WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("CreatedOn").HasColumnType("datetime").IsNotNullable();

Unfortunately, the datetime to DateTimeOffset conversion throws an exception.

 Is it possible to map a datetime database field to a DateTimeOffset property? I noticed a WithConverter<> extension. Would creating a custom AdoTypeConverter class do the trick?

 

 Thanks,

-Cory

 

2 Answers, 1 is accepted

Sort by
0
Yavor Slavchev
Telerik team
answered on 21 Oct 2015, 07:26 AM
Hi Cory,

It is possible to map DateTimeOffset field to a DateTime sql type using a converter.
You can read this article how you can implement and wire up an AdoTypeConverter and I will provide you with a sample implementation how you can convert DateTimeOffset to DateTime and vise versa.
In order to convert the DateTime to a DateTimeOfffset value when you are reading from the database, you could use the following code in the Read() method:
public override object Read(ref DataHolder holder)
{
    bool isNull = holder.Reader.IsDBNull(holder.Position);
 
    holder.NoValue = isNull;
 
    if (isNull)
    {
        holder.ObjectValue = DateTimeOffset.MinValue;
    }
    else
    {
        DateTime value = (DateTime) holder.Reader.GetValue(holder.Position);
        DateTime.SpecifyKind(value, DateTimeKind.Utc);
 
        DateTimeOffset utcDateTimeOffset = value;
        holder.ObjectValue = utcDateTimeOffset;
    }
    return (holder.Box) ? holder.ObjectValue : null;
}
In order to convert the DateTimeOffset back to DateTime when you are saving in the database, you could use the following code in the Write() method of the converter:
public override void Write(ref DataHolder holder)
{
    holder.Parameter.DbType = System.Data.DbType.DateTime;
    if (!holder.NoValue)
    {
        DateTimeOffset dateTimeOffset = (DateTimeOffset)holder.ObjectValue;
        DateTime date = dateTimeOffset.Date;
        holder.Parameter.Value = date;
    }
}

I hope this will help you to resolve the issue. Let me know if you need further assistance.

Regards,
Yavor Slavchev
Telerik
 
Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
0
Cory
Top achievements
Rank 1
answered on 21 Oct 2015, 11:29 AM

Awesome​, I'll give it a try.

 Thanks!

 -Cory

Tags
Databases and Data Types
Asked by
Cory
Top achievements
Rank 1
Answers by
Yavor Slavchev
Telerik team
Cory
Top achievements
Rank 1
Share this question
or