Geo*_*ell 4 c# azure azure-table-storage
使用azure表,如果我知道实体的RowKey和PartitionKey(因此我可以检索该实体),如何编辑该实体上的特定属性值?
这听起来像一个非常标准的操作,但正常的做法是这样的:
public void UpdateEntity(ITableEntity entity)
{
TableOperation replaceOperation = TableOperation.Replace(entity);
table.Execute(replaceOperation);
}
Run Code Online (Sandbox Code Playgroud)
即整个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);
}
Run Code Online (Sandbox Code Playgroud)
我的理解是,在幕后,行被存储为一组与该行上的属性相对应的键值对,因此更新属性的值应该很容易,而不必定义从TableEntity派生的整个C#类来执行此操作.
我该怎么做?
Geo*_*ell 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);
}
Run Code Online (Sandbox Code Playgroud)
而不是"替换"操作,执行"合并"操作(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);
}
Run Code Online (Sandbox Code Playgroud)
下面是一个更完整的例子.在这里,我首先创建了一名员工,然后只更改了该员工的"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);
Run Code Online (Sandbox Code Playgroud)
您也可以尝试InsertOrMerge(http://msdn.microsoft.com/en-us/library/windowsazure/microsoft.windowsazure.storage.table.tableoperation.insertormerge.aspx)操作.
归档时间: |
|
查看次数: |
7038 次 |
最近记录: |