给定PartitionKey,RowKey,属性名称和值,如何更新该实体的属性值?

时间:2013-02-06 15:17:58

标签: c# azure azure-table-storage

使用azure表,如果我知道实体的RowKey和PartitionKey(因此我可以检索该实体),如何编辑该实体上的特定属性值?

这听起来像是一个非常标准的操作,但正常的做法是:

public void UpdateEntity(ITableEntity entity)
{
    TableOperation replaceOperation = TableOperation.Replace(entity);
    table.Execute(replaceOperation);
}

即。整个C#TableEntity对象作为替换对象而不是单独的属性名称/值对。

我想要更像的东西:

public void UpdateEntityProperty<T>(string partitionKey, string rowKey,
                                string propertyName, T newPropertyValue)
{
    TableOperation retrieveOperation = TableOperation.Retrieve(partitionKey, rowKey);
    TableResult retrievedResult = table.Execute(retrieveOperation);
    TableEntity entity = (TableEntity)retrievedResult.Result;

    // This  line, of course, doesn't compile. But you get the idea.
    entity.SetPropertyValue(propertyName, newPropertyValue);

    TableOperation replaceOperation = TableOperation.Replace(entity);
    table.Execute(replaceOperation);
}

我的理解是在幕后,行被存储为一组与该行上的属性相对应的键值对,因此更新属性的值应该很容易,而不必定义从TableEntity派生的整个C#类。这样做。

我该怎么做?

3 个答案:

答案 0 :(得分:10)

为了完整起见,这是我最终使用的内容,灵感来自Gaurav Mantri的回答:

public void UpdateEntityProperty(string partitionKey, string rowKey,
                                 string propertyName, string newPropertyValue)
{
    var entity = new DynamicTableEntity(partitionKey, rowKey);
    var properties = new Dictionary<string, EntityProperty>();
    properties.Add(propertyName, new EntityProperty(newPropertyValue));
    var mergeOperation = TableOperation.Merge(entity);
    table.Execute(mergeOperation);
}

答案 1 :(得分:9)

执行“合并”操作(http://msdn.microsoft.com/en-us/library/windowsazure/microsoft.windowsazure.storage.table.tableoperation.merge),而不是“替换”操作。合并操作将确保仅更改正在修改的属性,而不更改所有其他属性。

public void UpdateEntityProperty<T>(string partitionKey, string rowKey,
                                    string propertyName, T newPropertyValue)
{
    TableOperation retrieveOperation = TableOperation.Retrieve(partitionKey, rowKey);
    TableResult retrievedResult = table.Execute(retrieveOperation);
    TableEntity entity = (TableEntity)retrievedResult.Result;

    // This  line, of course, doesn't compile. But you get the idea.
    entity.SetPropertyValue(propertyName, newPropertyValue);

    TableOperation mergeOperation = TableOperation.Merge(entity);
    table.Execute(mergeOperation);
}

下面有一个更完整的例子。在这里,我首先创建了一名员工,然后只更改了该员工的“MaritalStatus”属性:

CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;

CloudTable table = storageAccount.CreateCloudTableClient().GetTableReference("Employee");
DynamicTableEntity entity = new DynamicTableEntity()
{
    PartitionKey = "Employee",
    RowKey = "01",
};
Dictionary<string, EntityProperty> properties = new Dictionary<string, EntityProperty>();
properties.Add("Name", new EntityProperty("John Smith"));
properties.Add("DOB", new EntityProperty(new DateTime(1971, 1, 1)));
properties.Add("MaritalStatus", new EntityProperty("Single"));
entity.Properties = properties;

TableOperation insertOperation = TableOperation.Insert(entity);
table.Execute(insertOperation);

DynamicTableEntity updatedEntity = new DynamicTableEntity()
{
    PartitionKey = "Employee",
    RowKey = "01",
    ETag = "*",
};
Dictionary<string, EntityProperty> newProperties = new Dictionary<string, EntityProperty>();
newProperties.Add("MaritalStatus", new EntityProperty("Married"));
updatedEntity.Properties = newProperties;
TableOperation mergeOperation = TableOperation.Merge(updatedEntity);
table.Execute(mergeOperation);

您也可以尝试InsertOrMerge(http://msdn.microsoft.com/en-us/library/windowsazure/microsoft.windowsazure.storage.table.tableoperation.insertormerge.aspx)操作。

答案 2 :(得分:0)

试试这个:

if (entity != null)
{
     entity.propertyName = newPropertyValue;
     TableOperation updateOperation = TableOperation.Replace(entity);
     table.Execute(updateOperation);
}