【问题标题】:How do I get items from CosmosDB where the document property is lowercase, but the model property is uppercase?如何从 CosmosDB 中获取文档属性为小写但模型属性为大写的项目?
【发布时间】:2020-06-17 16:40:37
【问题描述】:

我的 cosmos 数据库中有一个名为“用户”的文档,其分区键为“电子邮件”。我正在使用 GetItemLinqQueryable() 函数通过 Azure 函数检索 cosmos 数据。类型 T 是 UserData 类型,如下所示:

public class UserData
    {
        public string Id { get; set; }
        public string Email { get; set; }
        public bool isAdmin { get; set; }
    }

我的 cosmos db 容器被称为“用户”,并且具有如下架构:

{
    "id": "2",
    "email": "ckelly@gmail.com",
    "isAdmin": false
}

我正在调用 GetItemLinqQueryable,但由于文档属性和 UserData 属性之间的大小写不同,它无法检索值。有没有办法可以在不区分大小写的情况下进行映射?

【问题讨论】:

    标签: c# azure-functions azure-cosmosdb


    【解决方案1】:

    你能用这样的模型吗?

        public class Item
    {
        [JsonProperty(PropertyName = "id")]
        public string Id { get; set; }
    
        [JsonProperty(PropertyName = "category")]
        public string Category { get; set; }
    
        [JsonProperty(PropertyName = "name")]
        public string Name { get; set; }
    
        [JsonProperty(PropertyName = "description")]
        public string Description { get; set; }
    
        [JsonProperty(PropertyName = "isComplete")]
        public bool Completed { get; set; }
    }
    

    也需要添加这个。

    using Newtonsoft.Json;
    

    【讨论】:

      猜你喜欢
      • 2011-10-31
      • 1970-01-01
      • 2014-05-04
      • 1970-01-01
      • 1970-01-01
      • 2019-02-26
      • 2014-04-18
      • 1970-01-01
      • 2013-07-16
      相关资源
      最近更新 更多