Azure Tables and DateTime: some limitations

1 minute read

A nice thing I ran into yesterday: if you have a TableStorageEntity child class containing a DateTime property, you have two thing to remember:
  • You cannot use DateTime.Now, just should use DateTime.UtcNow when defining "now"
  • Make sure your DateTime does default to TableStorageConstants.MinSupportedDateTime (and not the standard DateTime.MinValue)
I had a class like this
using System;
using Microsoft.Samples.ServiceHosting.StorageClient;

namespace LocalJoost.CloudMapper.Data.MapBuild
{
  public class Job : TableStorageEntity
  {
    #region Properties
    public string JobId { get; set; }
    public string MapName { get; set; }

    public DateTime QueuedAt { get; set; }
    public DateTime StartedAt { get; set; }
    public DateTime FinishedAt { get; set; }
    #endregion

    public Job()
    {
      QueuedAt = DateTime.Now;
    }

    public override string PartitionKey
    {
      get{return MapName;}
      set{}
    }

    public override string RowKey
    {
      get{return JobId;}
      set
      { }
    }
  }
}
and I simply could not store it. "There was an error processing the request". Yeah. I was able to find out the restrictions mentioned above, changed the class to
using System;
using Microsoft.Samples.ServiceHosting.StorageClient;

namespace LocalJoost.CloudMapper.Data.MapBuild
{
  public class Job : TableStorageEntity
  {
    #region Properties
    public string JobId { get; set; }
    public string MapName { get; set; }

    public DateTime QueuedAt { get; set; }
    public DateTime StartedAt { get; set; }
    public DateTime FinishedAt { get; set; }
    #endregion

    public Job()
    {
      QueuedAt = DateTime.UtcNow;
      StartedAt = TableStorageConstants.MinSupportedDateTime;
      FinishedAt = TableStorageConstants.MinSupportedDateTime;
    }

    public override string PartitionKey
    {
      get{return MapName;}
      set{}
    }

    public override string RowKey
    {
      get{return JobId;}
      set
      { }
    }
  }
}
and then it worked. Thanks to Steve Marx for pointing me to the TableStorageConstants on twitter live while I was debugging