0

Here is Problem In Detail

I have one Azure Table in which I have multiple rows in that table but my requirement is to update a single property on the basis of unique id which helps to find or get the records and update the related property

I tried a lot but couldn't find any approach to resolve this problem.

Please if any other solution would be available to resolve this problem please share it will really help me a lot.

  • Does this answer your question? https://stackoverflow.com/questions/37368447/updating-entity-data-to-azure-storage-table. In short, you could use a DynamicEntity, set its PartitionKey and RowKey and the property to update, and ETag=* and call a Merge or InsertOrMerge method. – Martin Staufcik May 02 '20 at 18:27
  • @MartinStaufcik, can merge operation will be updating the existing value to the new value or it should add a new property and its value? – Rahul Dhoundiyal May 03 '20 at 17:11
  • The dynamic entity allows to update only selected properties. When the merge is called with a property that is not in the table yet, a new column is created for this value. – Martin Staufcik May 04 '20 at 07:11
  • @MartinStaufcik, Thanks for your efforts. as per my research on the azure table currently, don't have a direct solution to update the specific property instead of creating new reference over via insert or merge method. – Rahul Dhoundiyal May 05 '20 at 07:42

1 Answers1

1

According to my test, we can use merge operation to update the existing property value to the new value or add the new property. Meanwhile, the old properties will be retained even if the new entity didn't define new properties in new entity.

For example

CloudStorageAccount account = CloudStorageAccount.Parse(" connection string");
            CloudTableClient tableClient = account.CreateCloudTableClient();
            CloudTable table =tableClient.GetTableReference("people");
            DynamicTableEntity entity = new DynamicTableEntity("Jim", "Xu");

            TableOperation retrieveOperation = TableOperation.Retrieve<DynamicTableEntity>(entity.PartitionKey, entity.RowKey);
            TableResult result = await table.ExecuteAsync(retrieveOperation);
            DynamicTableEntity tableEntity = result.Result as DynamicTableEntity;

            var test = "";
            foreach (var pro in tableEntity.Properties) {

                Console.WriteLine(pro.Key);
                Console.WriteLine(pro.Value.StringValue);
                test = pro.Key;
            }
            Console.WriteLine("update the existing last property and add a new property");
            entity.Properties.Add(test, new EntityProperty("testvalue"));
            if (!(tableEntity.Properties.Keys.Contains("Age"))) {

                entity.Properties.Add("Age", new EntityProperty("20"));

            };
            TableOperation mergeOperation = TableOperation.InsertOrMerge(entity);
             await table.ExecuteAsync(mergeOperation);
            result = await table.ExecuteAsync(retrieveOperation);
            tableEntity = result.Result as DynamicTableEntity;
            foreach (var pro in tableEntity.Properties)
            {

                Console.WriteLine(pro.Key);
                Console.WriteLine(pro.Value.StringValue);
            }
            Console.Read();

enter image description here


Update

My original table enter image description here

My code

CloudStorageAccount account = CloudStorageAccount.Parse(" connection string");
           CloudTableClient tableClient = account.CreateCloudTableClient();
            CloudTable table =tableClient.GetTableReference("people");
            DynamicTableEntity entity = new DynamicTableEntity("Jim", "Xu");
            entity.Properties.Add("Age", new EntityProperty("21"));
            TableOperation mergeOperation = TableOperation.InsertOrMerge(entity);
            await table.ExecuteAsync(mergeOperation);

Result

enter image description here


update1

I update the email with partition key = Jim and Row key = Xu

CloudTableClient tableClient = account.CreateCloudTableClient();
            CloudTable table =tableClient.GetTableReference("people");
            DynamicTableEntity entity = new DynamicTableEntity("Jim", "Xu");
            entity.Properties.Add("Email", new EntityProperty("tets1@gamil.com"));
            TableOperation mergeOperation = TableOperation.InsertOrMerge(entity);
            await table.ExecuteAsync(mergeOperation);

enter image description here

Community
  • 1
  • 1
Jim Xu
  • 21,610
  • 2
  • 19
  • 39
  • thanks for your efforts but my problem can't be resolved via merge method. I already tried this – Rahul Dhoundiyal May 05 '20 at 07:45
  • @RahulDhoundiyal Could you please the detailed message of errors? – Jim Xu May 05 '20 at 07:56
  • as such there was no error !! my above question is regarding an approach. and your snippet of code and logic must be right but according to my problem, there wasn't suitable because I want to update the property without adding new or similar property & as per my understanding merge operation can adding new or similar property they didn't replace the existing property value. – Rahul Dhoundiyal May 06 '20 at 05:35
  • @RahulDhoundiyalr please see my result. It can update the existing property value. – Jim Xu May 06 '20 at 07:01
  • It's better can you share the snippet of azure table view structure it is more clear the console view. – Rahul Dhoundiyal May 07 '20 at 20:14
  • in the same example can you replace one property key "Age" value which is currently have null value on which that partition key = Jack and Row key = Xu. can you update the same record with some other value without inserting another record for the same. – Rahul Dhoundiyal May 12 '20 at 09:22