繁体   English   中英

Azure 表存储 - 保存工作但检索数据 - 无法获取记录中的所有字段

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

我有以下 class 代表我的存储表中的记录:

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; }
  }
  
}

这就是 WorkspaceRequest 的样子:

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; }
  }

}

当我将数据保存到存储表时,它会正确地为我创建所有各种键/值。 但是当我尝试检索数据时,我没有得到工作区的详细信息。

这就是我得到的:

{
    "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
}

这部分是我如何保存到表中:

   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);

这是检索记录的方法的样子:

    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;
        }
    }

当我单步执行代码时,我可以看到“entity”有一个 WorkspaceRequest 类型的“workspace”属性,但它是 null。状态和 requestId 没问题。

你能告诉我哪里出错了吗?

谢谢。

编辑 1

为了提供帮助,这是我正在检索的实体 object 的调试 output:

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

这是一个屏幕截图,显示了如何将所有工作区数据保存在单个键/值对中:

在此处输入图像描述

我在调用 GetEntity 时通过更改逻辑取得了一些进展。 我没有将其强制转换为,而是将其视为一个,然后我将尝试手动将表实体 map 设置为供应记录。

所以换句话说,我改变了这个:

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

对此:

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

现在,当我对表实体进行调试时,这就是我所看到的:

在此处输入图像描述

因此,我将创建一个新方法,以手动将 map 从 TableEntity 转换为我的自定义类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM