无法从 LiteDb 存储和检索数据

Unable to store and retrieve data from LiteDb

我正在尝试将一个对象写入 LiteDb 并读回。 我尝试了以下代码,但不知何故 LiteDbParamEntry 对象的所有参数都返回为 NULL 或零。

public class LiteDbParamEntry
{
    [BsonId]
    public int Id
    {
        get { return (Index * 0x100) + SubIndex; }
    }

    public ushort Index;

    public ushort SubIndex;

    public string Text;
}

public class LiteDbParamValueStorage
{
    private const string _liteDbPath = "MyLiteData.db";

    public LiteDbParamValueStorage()
    {
        WriteEntry(123, 25);
        ReadEntry(123, 25);
    }

    public void WriteEntry(ushort index, ushort subIndex)
    {
        using (var db = new LiteDatabase(_liteDbPath))
        {
            var entry = new LiteDbParamEntry {Index = index, SubIndex = subIndex, Text = "SomeText"};

            var entries = db.GetCollection<LiteDbParamEntry>("LiteDbParamEntry");

            entries.Insert(entry);
        }
    }

    public void ReadEntry(ushort index, ushort subIndex)
    {
        using (var db = new LiteDatabase(_liteDbPath))
        {
            var collection = db.GetCollection<LiteDbParamEntry>("LiteDbParamEntry");
            var paramEntry = collection.FindById((index * 0x100) + subIndex);
            if (paramEntry != null)
                Console.WriteLine(paramEntry.Text);          //paramEntry.Text is returned as Null
            else
                return;
        }
    }
}

有人能给我指明正确的方向吗?

最后,我发现你的代码有什么问题...你忘记在字段声明的右侧添加 { get; set; } 指令 ;)

看这里:

public class LiteDbParamEntry
{
    [BsonId]
    public int Id
    {
        get { return (Index * 0x100) + SubIndex; }
    }

    public ushort Index { get; set; } //getter/setter added

    public ushort SubIndex  { get; set; } //getter/setter added

    public string Text  { get; set; } //getter/setter added
}

祝你好运!