/// <summary>
        /// Gets the sequence for a given table.
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public SequenceIdStore GetSequenceIdStore(string tableName)
        {
            var schema = SchemaTableProxy.Get(tableName.GetValidPartitionKey(), tableName.GetValidRowKey());

            if (schema == null || schema.Entity == null)
            {
                return(null);
            }
            var sequence = SequenceTableProxy.Get(tableName.GetValidPartitionKey(), tableName.GetValidRowKey()) ??
                           CreateSequenceFromSchema(tableName);

            return(new SequenceIdStore
            {
                FinalCachedId = sequence.Entity.Properties[PropertyFinalCachedId].StringValue,
                Schema = schema.Entity
            });
        }
        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);
        }