Search code examples
c#azureazure-table-storageazure-tablequery

Azure Table Storage - Save is working but retrieving data - can't get all fields in a record


I have the following class that represents a record in my storage table:

using Newtonsoft.Json;
using Azure.Data.Tables;
using System;
using Azure;

namespace MyProject.Models
{
    public class ProvisioningRecord:ITableEntity
  {
    public string PartitionKey { get; set;}
    public string RowKey { get; set; }
    public DateTimeOffset? Timestamp { get; set; }
    public ETag ETag {get;set; }

    [JsonProperty(PropertyName = "requestId")]
     public string requestId { get; set; }

    [JsonProperty(PropertyName = "status")]
    public string status { get; set; }

    [JsonProperty(PropertyName = "request")]
    public WorkspaceRequest workspace { get; set; }
  }
  
}

This is what WorkspaceRequest looks like:

using Newtonsoft.Json;
using System;

namespace MyProject.Models
{
  public class WorkspaceRequest
  {
    [JsonProperty(PropertyName = "name")]
     public string name { get; set; }

    [JsonProperty(PropertyName = "dedicated")]
    public string dedicated { get; set; }

    [JsonProperty(PropertyName = "alias")]
    public WorkspaceAlias[] Alias { get; set; }
  }
  public class WorkspaceAlias
  {
      [JsonProperty(PropertyName = "name")]
      public string name { get; set; }
  }

}

When I save the data to the storage table, it correct creates all the various key/values for me. But when I try to retrieve the data, I'm not getting the details of the workspace back.

This is what I get back:

{
    "partitionKey": "mypartitionkey",
    "rowKey": "85ffa92e-41ec-43ed-a01c-67eae99d7a83",
    "timestamp": "2022-02-24T15:17:57.6496818+00:00",
    "eTag": {},
    "requestId": "85ffa92e-41ec-43ed-a01c-67eae99d7a83",
    "status": "enqueued",
    "request": null
}

THis is in part, how I save to the table:

   public async Task<WorkspaceResponse> AddProvisioningRequest(WorkspaceRequest request, string requestId)   
 {
          //stuff.... 

            var entity = new TableEntity(provisioningRecord.status,provisioningRecord.requestId)
            {
                {"requestId",provisioningRecord.requestId.ToString()},
                {"status",provisioningRecord.status.ToString()},
                {"workspace",JsonConvert.SerializeObject(provisioningRecord.workspace)}
            };
            await tableClient.AddEntityAsync(entity);

This is what the method to retrieve records looks like:

    public async Task<ProvisioningRecord> GetWorkspaceRequest(string requestId)            
    {
        ProvisioningRecordentity = new();           
        try
        {                
            GetStorageAccountConnectionData(); 
            var serviceClient = new TableServiceClient(
                new Uri(connection.storageUri),
                new TableSharedKeyCredential(connection.storageAccountName, connection.storageAccountKey));
            var tableClient = serviceClient.GetTableClient(connection.tableName);
            entity = tableClient.GetEntity<ProvisioningRecord>(
                                "mypartitionKey",
                                requestId);                  
            return entity;
        } 
        catch (Exception ex)
        {
            Console.Write(ex.Message);
            return entity;
        }
    }

When I step through the code, I can see that "entity" has a "workspace" property of type WorkspaceRequest, but it's null. The status and requestId are ok though.

Can you show me where I've gone wrong?

Thanks.

Edit 1

To help, here's a debug output of the entity object I'm retrieving:

entity
{MyProject.Models.ProvisioningRecord}
ETag [ETag]:{W/"datetime'2022-02-24T15%3A17%3A57.6496818Z'"}
PartitionKey [string]:"myPartitionKey"
RowKey [string]:"85ffa92e-41ec-43ed-a01c-67eae99d7a83"
Timestamp:{2022-02-24 3:17:57 PM +00:00}
requestId [string]:"85ffa92e-41ec-43ed-a01c-67eae99d7a83"
status [string]:"enqueued"
workspace [WorkspaceRequest]:null

Here's a screenshot showing how all workspace data is saved in a single key /value pair:

enter image description here


Solution

  • I've made some progress by changing my logic when I call the GetEntity. Instead of casting it to , I treat it like a , and then I will try to manually map the table entity to a provisioning record.

    So in other words, I changed this:

     entity = tableClient.GetEntity<ProvisioningRecord>(
                                    "myPartitionKey",
                                    requestId);     
    

    To this:

     entity = tableClient.GetEntity<TableEntity>(
                                    "enqueued",
                                    requestId); 
    

    And now, when I do a debug on the table entity, this is what I see:

    enter image description here

    So I'm going to just create a new method to manually map from TableEntity to my custom type.