我正在从
Microsoft.Azure.Cosmos.Table
迁移到 Azure.Data.Tables
,遵循 this
使用 Cosmos,我有这个方法来插入/合并并返回插入的记录
private async Task<OfficeSupplyEntity?> InsertOrMergeAsync(CloudTable table, OfficeSupplyEntity record)
{
var insertOrMergeOperation = TableOperation.InsertOrMerge(record);
var result = await table.ExecuteAsync(insertOrMergeOp);
var insertedCustomer = result?.Result as OfficeSupplyEntity;
return insertedCustomer;
}
现在我想将其转换为使用 Azure.Data.Tables。但我不知道如何返回插入的记录。这里
newRecord
似乎得到了 Azure.Response
类型。如何提取插入的记录。
private async Task<OfficeSupplyEntity?> InsertOrMergeAsync(TableClient table, OfficeSupplyEntity record)
{
var insertedCustomer = await table.UpsertEntityAsync(record);
//how to return inserted?
}
帮助表示感谢
如何提取插入的记录。
您可以使用以下方法从 Azure Cosmos DB 表 API 检索插入的记录。它使用
GetEntityAsync
从表中检索插入的数据,如下面的输出所示。
private static async Task Main(string[] args)
{
string connectionString = "*****";
string tableName = "ftab";
var tableClient = new TableClient(connectionString, tableName);
var entity = new TableEntity("partitionKey", "rowKey")
{
{ "Name", "Pavan" },
{ "Age", "26" }
};
await tableClient.UpsertEntityAsync(entity);
TableEntity retrievedEntity = await tableClient.GetEntityAsync<TableEntity>("partitionKey", "rowKey");
Console.WriteLine($"PartitionKey: {retrievedEntity.PartitionKey}");
Console.WriteLine($"RowKey: {retrievedEntity.RowKey}");
foreach (var property in retrievedEntity)
{
Console.WriteLine($"{property.Key}: {property.Value}");
}
}
输出:
PartitionKey: partitionKey
RowKey: rowKey
odata.etag: W/"datetime'2024-08-23T12%3A06%3A26.9334605Z'"
PartitionKey: partitionKey
RowKey: rowKey
Name: Pavan
Age: 26
Timestamp: 8/23/2024 12:06:26 PM +00:00