private TableEntityProxy<DynamicTableEntity> CreateSequenceFromSchema(string tableName)
{
var schema = SchemaTableProxy.Get(tableName.GetValidPartitionKey(), tableName.GetValidRowKey());
if (schema == null)
throw new ArgumentException(String.Format("Sequence '{0}' is not defined. Cannot get the latest reserved key value.", tableName));
var sequence = new TableEntityProxy<DynamicTableEntity>(new DynamicTableEntity(schema.PartitionKey, schema.RowKey, "",
new Dictionary<string, EntityProperty>()));
sequence.Entity.Properties.Add(PropertyFinalCachedId, new EntityProperty(schema.Entity.SeedValue));
SequenceTableProxy.Insert(sequence.Entity);
//Insert would throw if another thread created after SequenceTableProxy.Get was called above. Only one thread can create the sequence.
// - a lock would not help, since the other thread may be in another role (web or worker) instance
return sequence;
}