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

ArgumentNullException in default example #1332

Open lazlo-bonin opened 5 years ago

lazlo-bonin commented 5 years ago

Just started testing out LiteDB and I can't get the basic example working without any change.

I already had to add Mode=Exclusive to the connection string to get it working on OSX.

Now I get a ANE a few lines later.

LiteDB v.4.1.4 installed from NuGet. OSX 10.14.5 Mojave.

Full error:

Exception has occurred: CLR/System.ArgumentNullException
An unhandled exception of type 'System.ArgumentNullException' occurred in LiteDB.dll: 'Value cannot be null.'
   at LiteDB.FileDiskService..ctor(String filename, FileOptions options)
   at LiteDB.LiteDatabase.<>c__DisplayClass11_0.<.ctor>b__0()
   at LiteDB.LazyLoad`1.get_Value()
   at LiteDB.LiteCollection`1.Insert(T document)
   at TestLiteDB.Program.Main(String[] args) in /Users/lazlo/Projects/TestLiteDB/Program.cs:line 35

Full Code:

using System;
using LiteDB;

namespace TestLiteDB
{
    // Basic example
    public class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string[] Phones { get; set; }
        public bool IsActive { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {

            // Open database (or create if not exits)
            using (var db = new LiteDatabase(@"MyData.db, Mode=Exclusive"))
            {
                // Get customer collection
                var customers = db.GetCollection<Customer>("customers");

                // Create your new customer instance
                var customer = new Customer
                {
                    Name = "John Doe",
                    Phones = new string[] { "8000-0000", "9000-0000" },
                    IsActive = true
                };

                // Insert new customer document (Id will be auto-incremented)
                customers.Insert(customer);

                // Update a document inside a collection
                customer.Name = "Joana Doe";

                customers.Update(customer);

                // Index document using a document property
                customers.EnsureIndex(x => x.Name);

                // Use Linq to query documents
                var results = customers.Find(x => x.Name.StartsWith("Jo"));
            }
        }
    }
}
lazlo-bonin commented 5 years ago

Just tested on v5 alpha 2, I get this error instead:

Exception has occurred: CLR/System.ArgumentException
An unhandled exception of type 'System.ArgumentException' occurred in System.Private.CoreLib.dll: 'EngineSettings must have Filename or DataStream as data source'
   at LiteDB.Engine.EngineSettings.CreateDataFactory()
   at LiteDB.Engine.DiskService..ctor(EngineSettings settings)
   at LiteDB.Engine.LiteEngine..ctor(EngineSettings settings)
   at LiteDB.ConnectionString.CreateEngine()
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1.CreateValue()
   at LiteDB.LiteCollection`1.Insert(T document)
   at TestLiteDB.Program.Main(String[] args) in /Users/lazlo/Projects/TestLiteDB/Program.cs:line 35
mbdavid commented 5 years ago

try fix your connection string with:

using (var db = new LiteDatabase(@"fiename=MyData.db; Mode=Exclusive"))