簡體   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