Copy Table Fails when multiple PartionKey in table

Jun 24, 2014 at 8:17 PM
The copy table functionality sometimes fails if there is more than on PartitionKey in the table. The code batches changes in groups of 10, but if there is more than one partitionkey in a particular group, then the process fails.

Here is an update to StorageAccountViewModel.cs that fixes the problem. It assumes that PartitionKeys are case sensitive.
                List<string> loPartitionKeyList = new List<string>();
                foreach (GenericEntity entity in entities)
                {
                    if (!loPartitionKeyList.Contains(entity.PartitionKey))
                    {
                        loPartitionKeyList.Add(entity.PartitionKey);
                    }

                    entityList.Add(entity);
                }

                tableClient = CloudStorageAccount.CreateCloudTableClient();
                tableServiceContext = CreateTableServiceContext(tableClient);

                const int batchSize = 10;
                int entitiesCopiedCount = 0;
                foreach (string lsPartionKey in loPartitionKeyList)
                {

                    int batchRecords = 0;
                    foreach (GenericEntity entity in entityList)
                    {
                        if (entity.PartitionKey.Equals(lsPartionKey))
                        {
                            tableServiceContext.AddObject(DestTableName, new GenericEntity(entity));
                            entitiesCopiedCount++;
                            batchRecords++;
                            if (batchRecords >= batchSize)
                            {
                                tableServiceContext.SaveChanges(SaveChangesOptions.Batch);
                                batchRecords = 0;
                            }
                        }
                    }
                    if (batchRecords > 0)
                    {
                        tableServiceContext.SaveChanges(SaveChangesOptions.Batch);
                    }

                }