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!
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); //
}