mbdavid / LiteDB

LiteDB - A .NET NoSQL Document Store in a single data file
http://www.litedb.org
MIT License
8.62k stars 1.25k forks source link

[QUESTION] database doesn't persist between sessions #2377

Open Jodsderechte opened 1 year ago

Jodsderechte commented 1 year ago

I've been using liteDB to store some values. However i tried setting up a 2. db to save some different values and those ones do not persist between application restarts. Embeds does persist. General settings do not. I have no idea why this happens or where the difference is. During the session i can save and read the values without any problems but after restarting values are lost.

string botToken = "SOMEVALUE";
DBhelper.addGeneralSetting(botToken);
public DataBaseHelper()
{
    db = new LiteDatabase(@"Embeds.db");
    // Get collection
    collection = db.GetCollection<ServerEmbed>("Embeds");
    // Create unique index in MessageID field
    collection.EnsureIndex(x => x.MessageID, true);
    settingDb = new LiteDatabase(@"GeneralSettings.db");
    generalSettings = settingDb.GetCollection<GeneralSetting>("GeneralSettings");
    generalSettings.EnsureIndex(x=> x.Setting, true);
}
public void addToDatabase(ServerEmbed embed)
{
    collection.Insert(embed);
}
public void addGeneralSetting(GeneralSetting setting)
{
    generalSettings.Insert(setting);
}

public class GeneralSetting
{
    [BsonField("Setting")]
    public string Setting { get; set; }
    [BsonField("Value")]
    public object Value { get; set; }

    public GeneralSetting(string setting, object value)
    {
        Setting = setting;
        Value = value;
    }
    public GeneralSetting() { }

    public override string ToString()
    {
        return $"Key: {Setting} + Value {Value.ToString()}";
    }
}
dethknite commented 1 year ago

There is no code here committing anything to the database. An simple example employing the using() { } is on the homepage of the project [How to use LiteDB] (https://github.com/mbdavid/LiteDB#how-to-use-litedb)

Jodsderechte commented 1 year ago

unles si'm completly blind that looks pretty much exactly like the exmple 🤔

var db = new LiteDatabase()
var col = db.GetCollection<Class>()
col.EnsureIndex(x => x.Somevalue, true);
 col.Insert(Object)

Although i have to admit i didn't copy over the addGeneralSetting() part from my main. But i'm doing that