如何更新azure表实体中的单个属性

sou*_*ura 2 asp.net-mvc azure azure-table-storage

嗨我正在研究azure表"T1",这个表有实体集列表

Primarykey  RowKey   P1  P2
PP          R1       5   10
PP          R2       6   11
Run Code Online (Sandbox Code Playgroud)

现在假设我只想更新P2属性并且不想触摸P1属性这可能是为了更新azure表实体中的单个属性. 记住我不想触摸P1属性因为它不断更新其他功能

Gau*_*tri 9

您想要执行的操作是Merge Entity.从REST API文档:

合并实体操作通过更新实体的属性来更新现有实体.此操作不会像更新实体操作那样替换现有实体.

这是您可以使用的示例代码:

    static void MergeEntityExample()
    {
        var cred = new StorageCredentials(accountName, accountKey);
        var account = new CloudStorageAccount(cred, true);
        var client = account.CreateCloudTableClient();
        var table = client.GetTableReference("TableName");
        var entity = new DynamicTableEntity("PartitionKey", "RowKey");
        entity.ETag = "*";
        entity.Properties.Add("P2", new EntityProperty(12));
        var mergeOperation = TableOperation.Merge(entity);
        table.Execute(mergeOperation);
    }
Run Code Online (Sandbox Code Playgroud)

上面的代码只会更新实体中的"P2"属性,不会触及其他属性.