An easy way to manage this is to reset the default date for any DateTime properties before writing to Azure Table Storage. The way I chose to do this was to add a function to the WritingEntity event in the table context. See the code below:
public class SafeDateContext : TableServiceContext
{
private static MinAzureUtcDate = new DateTime(1601, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public SafeDateContext(CloudStorageAccount account)
: base(account.TableEndpoint.ToString(), account.Credentials)
{
WritingEntity += new EventHandler(FixDates);
}
private void FixDates(ReadingWritingEntityEventArgs e)
{
// The XML of the properties already being sent to Azure
XElement properties = e.Data.Descendants(Meta + "properties").First();
foreach (var p in properties.Elements())
{
string type = p.Attribute(Meta + "type") == null ? null : p.Attribute(Meta + "type").Value;
bool isNull = string.Equals("true", p.Attribute(Meta + "null") == null ? null : p.Attribute(Meta + "null").Value, StringComparison.OrdinalIgnoreCase);
if (!isNull && type == "Edm.DateTime")
{
string value = p.Value;
DateTime dateValue = (DateTime)ConvertType(type, value, isNull);
if (dateValue < MinAzureUtcDate)
{
p.SetValue(MinAzureUtcDate);
}
}
}
}
No comments:
Post a Comment