c# - 在 Microsoft.Azure.Cosmos.Table 中使用 TableEntity.Flatten 的正确方法是什么?

标签 c# asp.net azure .net-core azure-cosmosdb

我正在尝试使用 Microsoft.Azure.Cosmos.Table 将 json 对象从我的 API 项目存储到 CosmosDB。 我的 Json 看起来像这样:

{
  "age": 0,
  "type": "network-error",
  "url": "https://www.example.com/",
  "body": {
    "sampling_fraction": 0.5,
    "referrer": "http://example.com/",
    "server_ip": "2001:DB8:0:0:0:0:0:42",
    "protocol": "h2",
    "method": "GET",
    "request_headers": {},
    "response_headers": {},
    "status_code": 200,
    "elapsed_time": 823,
    "phase": "application",
    "type": "http.protocol.error"
  }
}

我的 Cosmos 是 Table API 类型,因此我需要将其展平,但无法找到一种最通用的方法,MSDN 文档没有太多关于实现的内容。

非常感谢,感谢您的时间和帮助。

最佳答案

为您的模型创建一个 .Net 类并将其传递给 TableEntity.Flatten。然后,您可以使用返回的扁平键和 EntityProperty 值对的字典,通过指定分区键和行键来创建 DynamicTableEntity 类。您可以将其插入到 cosmos db 表存储中。

用法应该与表存储 api 中的使用方式类似。看看这里的单元测试: https://github.com/Azure/azure-storage-net/commit/daff940a506b27650901b9b9feb59131ffacce6d

ShapeEntity shapeEntity = new ShapeEntity(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "square", 4, 4);
            OperationContext operationContext = new OperationContext();

            DynamicTableEntity dynamicTableEntity = new DynamicTableEntity(shapeEntity.PartitionKey, shapeEntity.RowKey)
            {
                Properties = TableEntity.Flatten(shapeEntity, operationContext)
            };

您可以使用 SDK 将此 DynamicTableEntity 写入 Cosmos DB。

要将扁平化的DynamicTableEntity读回复杂对象,读取时需要使用TableEntity.ConvertBack api。

关于c# - 在 Microsoft.Azure.Cosmos.Table 中使用 TableEntity.Flatten 的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60498747/

相关文章:

azure - 数据工厂定价中的数据流

Azure数据工厂分隔文本文件忽略导入的架构

c# - 将字符串列表作为参数传递给控制台应用程序

c# - 使用 C# 在 Skype 上错过聊天

c# - 默认情况下,plinq 会对 Dictionary<string, string> 使用范围分区还是 block 分区?以及如何强制对方?

asp.net - MVC 3 通配符路由即 www.mydomain.com/username

asp.net - 数据库错误,供应商代码 17

c# - Entity Framework 包含命令 - 左连接还是内连接?

c# - 如何在新窗口中打开图像或pdf文件?

azure - 使用不记名 token 授权 Azure 请求?