0

我有一个要求,我必须更新 azure 表中同一行的不同列,该表由多个线程同时访问(所有查询同一行),而不会覆盖其他线程将更新的内容。 示例代码

*在此处输入图片描述*

   public void doSomething(){
List<string> records= new List<string>();

for (var j = 0; j < 12; j++)
            {
                InsertRecords(j);//Inserts data to the table and Creates a variable Ids=PartitionKey+rowkey
                records.Add(Ids);//It has all 12 partitionKeys and RowKeys combinations
            }
            for (var i = 0; i < 12;i+=4)
                        {
                            List<Task> tasks = new List<Task>();
                            tasks.Add(Task.Run(() => {
                                UpdateTable("COl1",records[i]); }));//should update col1 to complete
                            tasks.Add(Task.Run(() => {
                                UpdateTable("Col2",records[i]); }));
                            tasks.Add(Task.Run(() => {
                                UpdateTable("Col3",records[i]); }));
tasks.Add(Task.Run(() => {
                                UpdateTable("Col4",records[i]); }));
                            Task.WaitAll(tasks.ToArray());
                        }
            }
        public void UpdateTable(String message){
         ITableEntity entity = myRepo.retrieveEntity(myPartitionKey, myRowKey);            
            If (message=="COL1"){
            //Update Col1 to Complete
           (myClass)entity).Col= "Complete";
            }
            If (message=="COL2"){
            //Update Col2 to Complete
        (myClass)entity).Col2= "Complete";
            }
            If (message=="COL3"){
            //Update Col3 to Complete
        (myClass)entity).Col3= "Complete";
            }
         TableOperation tableOp=TableOperation.InsertOrReplace(entity);
    //var mergeOperation = TableOperation.Merge(entity); tried this also but didnt work
               // myTable.Execute(mergeOperation);
         CloudStorageAccount storageAccount = CloudStorageAccount.Parse("ConnString");
                    CloudTableClient tableClient=storageAccount.CreateCloudTableClient();
                    myTable = tableClient.GetTableReference("MyTable");           
                    myTable.CreateIfNotExists();
                    myTable.Execute(tableOp);//doesn't seem to work[overwrites]
        }

预期输出:COl1、Col2、Col3 的状态应为“完成”。如何做到这一点?

4

0 回答 0