1
votes

How can I assign default value to DateTime property in table entity of Azure Storage Table.

I am trying the code below, but I'm not able to set value (throwing an error from Azure Table while inserting, as DateTime taking has 1/1/0001).

[DefaultValue(DateTime.Now]

public DateTime LastUpdate { get; set; }
2

2 Answers

1
votes

As mentioned by @Aravind in the answer, minimum value for DateTime type attribute supported by Azure Tables is Jan 1, 1600 UTC. Thus setting the default value to DateTime.MinValue will result in an error.

One possible solution for you is to keep this attribute nullable in your model.

public DateTime? LastUpdate { get; set; }

So, when no value is supplied the default value for this would be null. You would need to remove DefaultValue attribute as mentioned in comments above.

1
votes

As Per msdn, the value that can go in datetime property is "A 64-bit value expressed as Coordinated Universal Time (UTC). The supported DateTime range begins from 12:00 midnight, January 1, 1601 A.D. (C.E.), UTC. The range ends at December 31, 9999."

So you cannot assign DateTime.MinValue. Lowest you can store is new DateTime(1601, 1, 1)