对于azure表,如果我知道实体的RowKey和PartitionKey(这样我就可以检索该实体),我如何编辑该实体上的特定属性值?
这听起来像是一个非常标准的操作,但正常的操作方式是:
public void UpdateEntity(ITableEntity entity)
{
TableOperation replaceOperation = TableOperation.Replace(entity);
table.Execute(replaceOperation);
}
即给出整个C#表实体对象作为替换而不是单个属性名称/值对。
我想要更像的东西
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#类
我该怎么做?
为了完整起见,以下是我最终使用的内容,灵感来自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);
}
不要执行"替换"操作,而是执行"合并"操作(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)操作。
试试这个:
if (entity != null)
{
entity.propertyName = newPropertyValue;
TableOperation updateOperation = TableOperation.Replace(entity);
table.Execute(updateOperation);
}