4

使用 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# 类这样做。

我该怎么做?

4

3 回答 3

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);
}
于 2013-02-08T11:54:24.547 回答
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 ) 操作。

于 2013-02-06T15:34:44.970 回答
0

尝试这个:

if (entity != null)
{
     entity.propertyName = newPropertyValue;
     TableOperation updateOperation = TableOperation.Replace(entity);
     table.Execute(updateOperation);
}
于 2013-02-06T15:27:12.197 回答