首页 > 解决方案 > LiteDB:使用类中没有 BsonID 的更新方法时出错

问题描述

我正在使用 LiteDB,当我在任何没有任何 BsonID 参数的类中使用 Update() 方法时

public class Test
{
    public string ServerSecretId { get; set; }
    public Dictionary<string, string> webhooks { get; set; }
}

抛出我: 未处理的异常。LiteDB.LiteException:字段“_id”上的 BSON 数据类型“Null”无效。

我不知道是什么原因造成的。这是代码:

{
        LiteDatabase db = new LiteDatabase(@"C:\Users\jesus\Downloads\database\db.db");

        var x = db.GetCollection<Test>("test");

        var y = new Test
        {
            ServerSecretId = "123456789",
            webhooks = new Dictionary<string, string>()
            {
                {
                    "webhook1", "c"
                }
            }
        };

        x.Insert(y);

        y.webhooks["webhook1"] = "a";

        x.Update(y);
}

标签: exceptionnulllitedb

解决方案


推荐阅读