0

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

dot
  • 14,928
  • 41
  • 110
  • 218
  • Do you see proper value for `workspace` property in Table Storage? You can view the data using Storage Explorer. My guess is that this property is not getting saved. – Gaurav Mantri Feb 24 '22 at 15:54
  • @GauravMantri so it looks like it's being saved but instead of a single key called "workspace", each property in the workspace class is being saved as a separate key/value pair, at the same level as the other fields. Does that make sense? or would a screenshot help? – dot Feb 24 '22 at 15:59
  • 1
    Screenshot would definitely help. Thanks. – Gaurav Mantri Feb 24 '22 at 16:02
  • @GauravMantri please see Edit 1. – dot Feb 24 '22 at 16:19
  • I think I need to explicitly deserialize? and cast? – dot Feb 24 '22 at 16:21

1 Answers1

0

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.

dot
  • 14,928
  • 41
  • 110
  • 218