0

这是详细的问题

我有一个 Azure 表,其中该表中有多行,但我的要求是根据唯一 id 更新单个属性,这有助于查找或获取记录并更新相关属性

我尝试了很多,但找不到任何方法来解决这个问题。

如果有任何其他解决方案可以解决这个问题,请分享它真的对我有很大帮助。

4

1 回答 1

1

根据我的测试,我们可以使用merge操作将现有属性值更新为新值或添加新属性。同时,即使新实体没有在新实体中定义新属性,旧属性也会保留。

例如

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();

在此处输入图像描述


更新

我原来的表 在此处输入图像描述

我的代码

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);

结果

在此处输入图像描述


更新1

我用分区键 = Jim 和 Row 键 = 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);

在此处输入图像描述

于 2020-05-04T02:16:49.483 回答