将默认值分配给 table 天蓝色存储 table 实体中的 DateTime 属性

assign default value to DateTime property in table entity of azure storage table

如何为 Azure 存储 Table 的 table 实体中的 DateTime 属性 分配默认值 Table。

我正在尝试下面的代码,但我无法设置值(插入时从 Azure Table 抛出错误,因为日期时间为 1/1/0001)。

[DefaultValue(DateTime.Now]

public DateTime LastUpdate { get; set; }

根据 msdn,日期时间 属性 中可以输入的值是 "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."

所以你不能分配DateTime.MinValue。您可以存储的最低值是 new DateTime(1601, 1, 1)

正如@Aravind 在回答中提到的,Azure Tables 支持的 DateTime 类型属性的最小值为 Jan 1, 1600 UTC。因此将默认值设置为 DateTime.MinValue 将导致错误。

一个可能的解决方案是在模型中保留此属性 nullable

public DateTime? LastUpdate { get; set; }

因此,当未提供任何值时,此值的默认值为 null。您需要删除上面评论中提到的 DefaultValue 属性。