我可以从TableOperation Retrieve操作中获取PartionKey和RowKey吗?

时间:2019-01-18 16:43:55

标签: c# azure-storage azure-table-storage

有一种方法可以从TableOperation参数中获取PartitionKey和RowKey

public override Task<TableResult> ExecuteAsync(TableOperation operation)
{
    switch (operation.OperationType)
    {
        case TableOperationType.Retrieve:
            //entityResult = Get<ITableEntity>(operation.PartitionKey, operation.RowKey);
            break;
    }
 }

2 个答案:

答案 0 :(得分:0)

Azure Table Storage Document进行了以下介绍:

PartitionKey属性

对表进行分区以支持跨存储节点的负载平衡。表的实体按分区组织。分区是拥有相同分区键值的实体的连续范围。分区键是给定表中分区的唯一标识符,由PartitionKey属性指定。分区键构成实体主键的第一部分。分区键可以是最大1 KB的字符串值。

您必须在每个插入,更新和删除操作中包含PartitionKey属性 操作。

RowKey属性

主键的第二部分是行键,由RowKey属性指定。行键是给定分区中实体的唯一标识符。 PartitionKeyRowKey共同唯一地标识表中的每个实体。 行键是一个字符串值,最大长度为1 KB。

每个插入,更新和删除操作中都必须包含RowKey属性。

因此,我们必须在每个插入,更新和删除操作中包括PartitionKeyRowKey属性。

TableOperation.Retrieve重载了三种方法: enter image description here

无论哪个Retrieve,都需要PartitionKeyRowKeyenter image description here

您无法从TableOperation Retrieve操作中获取PartionKeyRowKey

创建TableOperation时,它应包含实体的PartionKeyRowKey。 例如:

TableOperation retrieveOperation = TableOperation.Retrieve<CustomerEntity>("Smith", "Ben");

但是在您的方法中,TableOperation operation是一个参数,因此Entitynull

希望这可以为您提供帮助。

答案 1 :(得分:0)

是的,你可以,但你需要使用反射,可能还有另一种选择,但反射仍然是简单的实现

例如:var rowKey = GetField(tableOperationObject, "RowKey");

private string GetField(TableOperation operation, string field)
        {
            PropertyInfo highlightedItemProperty = operation.GetType()
                .GetProperties(BindingFlags.NonPublic  | BindingFlags.Instance).Single(pi => pi.Name == field);
            object value = highlightedItemProperty.GetValue(operation, null);
            
            return value?.ToString();
        }