You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从Azure.Data.Tables的UpsertEntityAsync获取插入记录?

从Microsoft.Azure.Cosmos.Table迁移到Azure.Data.Tables:Upsert后获取返回实体

在Azure.Data.Tables中,要实现和原InsertOrMergeAsync一样返回插入/合并后实体的功能,你需要使用泛型版本的UpsertEntityAsync重载,它会直接返回包含实体的Response<T>对象,从中即可提取最终的实体数据。

改造后的代码示例

private async Task<OfficeSupplyEntity?> UpsertAndReturnEntityAsync(TableClient tableClient, OfficeSupplyEntity record)
{
    // 使用TableUpdateMode.Merge对应原InsertOrMerge操作,若要完全替换现有实体则用Replace
    var response = await tableClient.UpsertEntityAsync(record, TableUpdateMode.Merge);
    // 从Response中获取服务器返回的最终实体
    return response.Value;
}

关键说明

  • 原InsertOrMerge对应Azure.Data.Tables中的TableUpdateMode.Merge模式:实体不存在则插入,存在则合并属性;若需要覆盖现有实体全部内容,可改用TableUpdateMode.Replace。
  • 泛型重载UpsertEntityAsync<T>会自动将服务器响应反序列化为你的实体类型,response.Value包含服务器生成的ETag等属性,能保证拿到的是服务器端的最终状态。
  • 如果你的实体没有服务器端生成的额外属性(除ETag外),也可以直接返回传入的record,但更推荐用response.Value确保数据一致性。

内容的提问来源于stack exchange,提问作者binga58

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 07:45:03