0

I've done a fair bit of research, but I can't find anyone who has the same problem as me (sadly). I am using LiteDB to create a NoSQL database.

When the program first runs and the database is created, the query in the example below works just fine. When I restart the program, it fails saying that it is null. The weird thing is, if I do a count it returns 8 records. So something exists - why can't I pull it out?

Here is my code:

public class ExternalTools
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string[] Types { get; set; }
}

public void GetAll()
{
     var localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
     var folderPath = localFolder.Path;
     var filePath = Path.Combine(folderPath, @"MyData4.db");

     using (var db = new LiteDatabase(filePath))
     {
          Tools = db.GetCollection<ExternalTools>("externalTools");

          if (Tools.Count() == 0)
          {
              CreateToolList();

              // Index document using document Name property
              Tools.EnsureIndex(x => x.Name);
            }
      }

      Debug.WriteLine(Tools.Count());
      var temp = Tools.FindAll(); // null error
      var test = Tools.FindById(1); // another null error
      Debug.WriteLine(test.Name); //

}

Thanks!

Yecats
  • 1,715
  • 5
  • 26
  • 40

1 Answers1

0

Well, I figured it out (so many hours of debugging wasted!) My code is in the wrong spot, if I move it into the using statement it works just fine. I suspect this has to do with the fact that on the first run it's adding stuff into the colection so it has the proper reference. Regardless, this code works:

        using (var db = new LiteDatabase(filePath))
        {
            Tools = db.GetCollection<ExternalTools>("externalTools");

            if (Tools.Count() == 0)
            {
                CreateToolList();

                // Index document using document Name property
                Tools.EnsureIndex(x => x.Name);
            }

            Debug.WriteLine(Tools.Count());
            var temp = Tools.FindAll(); // null error
            var test = Tools.FindById(1); // another null error
            Debug.WriteLine(test.Name); //
        }
Yecats
  • 1,715
  • 5
  • 26
  • 40
  • You always keep LiteDatabase open to access you data. If you are using "using" statment, will works only inside this code block. I will work in a better error message to user know that database must keep open. – mbdavid Jun 26 '18 at 12:58
  • Thank you! Is there a better way to keep it open outside of the "using" statement? – Yecats Jun 27 '18 at 13:04
  • You can not use "using" statement, You can keep open your LiteDatabase as long as you want, just call Dispose() when you "close/finish" your application. – mbdavid Jun 28 '18 at 13:47
  • I had the same issue, but all my properties were null because I only specified a getter... It somehow didn't throw an exception though, just didn't bother to set a value. – Johannes Mols Apr 02 '23 at 20:55