【问题标题】:Is There Any Method To Update Single Row Single Property in Azure Table?是否有任何方法可以更新 Azure 表中的单行单个属性?
【发布时间】:2020-08-17 05:18:16
【问题描述】:

这里有详细的问题

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

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

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

【问题讨论】:

  • 这能回答你的问题吗? stackoverflow.com/questions/37368447/…。简而言之,您可以使用 DynamicEntity,设置其 PartitionKey 和 RowKey 以及要更新的属性,以及 ETag=* 并调用 Merge 或 InsertOrMerge 方法。
  • @MartinStaufcik,合并操作可以将现有值更新为新值还是应该添加新属性及其值?
  • 动态实体只允许更新选定的属性。当使用尚未在表中的属性调用合并时,会为此值创建一个新列。
  • @MartinStaufcik,感谢您的努力。根据我目前对天蓝色表的研究,没有直接的解决方案来更新特定属性,而不是通过插入或合并方法创建新的引用。

标签: c# azure azure-functions azure-table-storage azure-tablequery


【解决方案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 和行键 = 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);

【讨论】:

  • 感谢您的努力,但我的问题无法通过合并方法解决。我已经试过了
  • @RahulDhoundiyal 能否请您详细说明错误信息?
  • 因此没有错误!!我的上述问题是关于一种方法的。并且您的代码和逻辑的 sn-p 必须是正确的,但是根据我的问题,不合适,因为我想在不添加新的或类似的属性的情况下更新属性,并且根据我的理解,合并操作可以添加新的或类似的属性没有替换现有的属性值。
  • @RahulDhoundiyalr 请看我的结果。它可以更新现有的属性值。
  • 能不能分享一下azure table view结构的sn-p,console view更清晰。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-31
  • 2015-10-03
  • 2014-04-17
  • 1970-01-01
  • 2022-01-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多