Azure 表存储 - 保存正在工作,但正在检索数据 - 无法获取记录中的所有字段
我有以下类代表存储表中的记录:
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;
}
}
当我单步执行代码时,我可以看到“实体”有一个“工作空间”属性类型为 WorkspaceRequest,但它为 null。不过 status 和 requestId 都可以。
你能告诉我我哪里出错了吗?
谢谢。
编辑 1
为了提供帮助,这里是我正在检索的实体对象的调试输出:
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
下面的屏幕截图显示了如何将所有工作区数据保存在单个键/值对中:
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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我调用 GetEntity 时,我通过更改逻辑取得了一些进展。我不会将其转换为 ,而是将其视为 ,然后我将尝试手动将表实体映射到配置记录。
换句话说,我将其更改
为:
现在,当我对表实体进行调试时,这就是我看到的:
所以我将创建一个新方法手动从 TableEntity 映射到我的自定义类型。
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:
To this:
And now, when I do a debug on the table entity, this is what I see:
So I'm going to just create a new method to manually map from TableEntity to my custom type.