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.