Azure / azure-cosmos-table-dotnet

.NET SDK for Azure Cosmos Table API
14 stars 6 forks source link

Returned partition and row keys from retrieve operation are null #41

Closed baharedankoyuncu closed 4 years ago

baharedankoyuncu commented 4 years ago

Hi,

I'm trying to fetch an entity from Azure Table Storage with the following code.

public async Task<T> GetAsync(string partitionKey, string rowKey)
{
    var operation = TableOperation.Retrieve<TableEntityAdapter<T>>(partitionKey, rowKey);
    var result = await _table.ExecuteAsync(operation);
    var complexEntity = result?.Result as TableEntityAdapter<T>;

    return complexEntity.OriginalEntity;
}

Inspecting the TableResult object namely result and looking into the Result property, I see the correct PartitionKey and RowKey properties. But the OriginalEntity property contain nulls for the partition and row key.

I can't seem to figure out why this happens but if I set the table operation to be Retrieve<T> instead of Retrieve<TableEntityAdapter<T>> then I get the correct keys except that now my I do not get the "complex entity".

I haven't been able to find anything about this behavior and currently blinded by this issue.

christopheranderson commented 4 years ago

Hi @baharedankoyuncu - thanks for reporting this issue.

We actually recommend not using TableEntityAdapter. Instead, we recommend directly extending TableEntity. TableEntityAdapter has some odd corner cases, as you've seen, that doesn't make it an ideal pattern.

I've created #58 to track deprecating this and then removing it in v2.

AnakinBing commented 3 years ago

Hi, Use Reflection.

public T Get(string partitionKey, string rowKey) { TableOperation operation = TableOperation.Retrieve<TableEntityAdapter>(partitionKey, rowKey); TableResult result = table.Execute(operation); TableEntityAdapter TableT = result.Result as TableEntityAdapter; T t = TableT.OriginalEntity; PropertyInfo[] propertyInfos = typeof(T).GetProperties(); foreach(PropertyInfo propertyInfo in propertyInfos) { if (propertyInfo.Name == "PartitionKey") propertyInfo.SetValue(t, TableT.PartitionKey); if (propertyInfo.Name == "RowKey") propertyInfo.SetValue(t, TableT.RowKey); if (propertyInfo.Name == "Timestamp") propertyInfo.SetValue(t, TableT.Timestamp); if (propertyInfo.Name == "ETag") propertyInfo.SetValue(t, TableT.ETag); } return t; }