Pregunta

I'm struggling with what I thought should be simple, I've a database table in an azure DB which has a column called "CreatedOn" the datatype of this column in database is "DateTimeOffset".

My timezone is IST(+5:30), i'm trying to populate values in this table, using Entity Framework through a RESTful WCF service using JsonSerializer.

The problem is that no offset value goes inside database when I make the service call through a windows phone app.

The property is declared as follows.

/// <summary>
    /// Gets or sets the created on.
    /// </summary>
    [DataMember]
    public DateTimeOffset? CreatedOn
    {
        get
        {
            return DateTime.Now;
        }

        set
        {
            this.OnPropertyChanged("CreatedOn");
        }
    }

The class knows "DateTimeOffset" through known types.

[KnownType(typeof(DateTimeOffset?))]
[DataContract]
public class MyTableItem : TableBase

At the time of populating the object, I've tried all of the following

tableItem.CreatedOn = DateTime.Now;
tableItem.CreatedOn = DateTime.UtcNow;
tableItem.CreatedOn = DateTime.SpecifyKind(DateTime.Now,DateTimeKind.Utc)

nothing works, what i get is shown in the image below. The offset is always 00:00, the ones which are showing correct values, are added without using azure WCF.

the image url is http://sdrv.ms/18Kz3HT

¿Fue útil?

Solución

This is the psueducode for the your implementation of the CreatedOn method:

  • When someone asks for the value, just give them the current date and time in their local time zone.

  • When someone gives me a value, ignore it and notify anyone that is subscribed that the property is changed.

I don't think either of these are what you intended. The only way you are seeing anything at all in SQL is because the current time is returned when the object is serialized.

I also don't think you need to use the [KnownType] attribute at all. DateTimeOffset is a native type, and nullable types should be understood by Entity Framework just fine.

So all you really need is this:

[DataMember]
public DateTimeOffset? CreatedOn {get; set;}

If you need INPC, then you would expand that to:

private DateTimeOffet? _createdOn;

[DataMember]
public DateTimeOffset? CreatedOn
{
    get
    {
        return _createdOn;
    }
    set
    {
        _createdOn = value;
        this.OnPropertyChanged("CreatedOn");
    }
}

When it comes to populating the object, just use:

tableItem.CreatedOn = DateTimeOffset.Now;

Now you should be able to properly persist any values you are given. But you might want to dig a bit deeper into the serialization though. If you're passing them in a format like /Date(123456789123)/ then you are dropping all offset information. You could use /Date(123456789123+0530)/ which will carry the offset, but it is still an ugly an non-standard format. What you really want to pass is an ISO8601 extended format string like 2013-08-11T01:23:45+05:30. You can do this by using the JSON.Net serializer instead of your current one.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top