1

我正在按照此文档将每个项目设置ttl为 CosmosDB 表条目。但是,当我在实体类中添加字段名称时ttl,我在进行插入/替换调用时遇到以下错误:

无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'System.Nullable`1[System.Int32]',因为该类型需要 JSON 原始值(例如字符串、数字、布尔值、null)正确反序列化。要修复此错误,请将 JSON 更改为 JSON 原始值(例如字符串、数字、布尔值、null)或将反序列化类型更改为正常的 .NET 类型(例如,不是整数等原始类型,而不是集合类型像数组或列表)可以从 JSON 对象反序列化。JsonObjectAttribute 也可以添加到类型中以强制它从 JSON 对象反序列化。路径 'ttl.$t',第 1 行,位置 109。

public class MyEntity : TableEntity
{
    public string Prop { get; set; }
    
    [JsonProperty(PropertyName = "ttl", NullValueHandling = NullValueHandling.Ignore)]
    public int? ttl { get; set; }

    public MyEntity(
       string pk,
       string rk,
       string prop)
    {
        this.PartitionKey = pk;
        this.RowKey = rk;
        this.Prop =prop;
        this.ttl = -1;
    }
}

如何解决?

4

1 回答 1

1

更新

按照官方文档更新我的项目Azure Cosmos DB Emulator。项目将根据 的预期值被删除TTL

while (feedIterator.HasMoreResults)
{
    foreach (var item in await feedIterator.ReadNextAsync())
    {
       item.ttl = 10;
       await container.UpsertItemAsync<MyEntity>(item, new PartitionKey(item.address));
    }
}

在此处输入图像描述

希望我的回答可以帮助到你。

在此处输入图像描述

using System;
using System.Threading.Tasks;
using System.Configuration;
using System.Collections.Generic;
using System.Net;
using System.Linq;
using Newtonsoft.Json;
using Microsoft.Azure.Cosmos;
using Microsoft.Azure.Cosmos.Table;

namespace CosmosGettingStartedTutorial
{
    class Program
    {
        // <Main>
        public static async Task Main(string[] args)
        {
            try
            {
                Console.WriteLine("Beginning operations...\n");
                CosmosClient client = new CosmosClient("https://localhost:8081/", "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==");
                Database database = await client.CreateDatabaseIfNotExistsAsync("ToDoList");
                Container container = database.GetContainer("jason");
                // Add for an item
                QueryDefinition queryDefinition = new QueryDefinition("select * from c ");
                FeedIterator<MyEntity> feedIterator = container.GetItemQueryIterator<MyEntity>(queryDefinition, null, new QueryRequestOptions() { PartitionKey = new PartitionKey("address0") });
                int count = 0;
                while (feedIterator.HasMoreResults)
                {
                    foreach (var item in await feedIterator.ReadNextAsync())
                    {
                        if (item.id == "0")
                        {
                            Console.WriteLine("id equal 0  is exist: id = " + item.id);
                            Console.WriteLine("We will change id='test" + item.id + "'");
                            Console.WriteLine("pls wait ,will update ");
                            item.id = "test" + item.id;
                            await container.UpsertItemAsync<MyEntity>(item, new PartitionKey(item.address));
                        }
                        count++;
                    }
                }

                int num = count + 5;
                for (int i = count; i < num; i++)
                {
                    MyEntity entity = new MyEntity(i.ToString(), i.ToString(), i.ToString());
                    entity.id = i.ToString();
                    entity.address = "address0";
                    await container.CreateItemAsync<MyEntity>(entity, new PartitionKey(entity.address));
                }
            }
            catch (CosmosException de)
            {
                Exception baseException = de.GetBaseException();
                Console.WriteLine("{0} error occurred: {1}", de.StatusCode, de);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}", e);
            }
            finally
            {
                Console.WriteLine("End of demo, press any key to exit.");
                Console.ReadKey();
            }
        }
        public class MyEntity : TableEntity
        {
            public string Prop { get; set; }

            [JsonProperty(PropertyName = "ttl", NullValueHandling = NullValueHandling.Ignore)]
            public int? ttl { get; set; }

            public MyEntity(string pk, string rk, string prop)
            {
                this.PartitionKey = pk;
                this.RowKey = rk;
                this.Prop = prop;
                this.ttl = -1;
            }
            public string address { get; set; }
            public string id { get; set; }
        }
    }
}
于 2020-07-21T03:32:13.327 回答