Litedb: Sequence contains no elements at 5.0.0-alpha

Created on 7 Aug 2019  路  8Comments  路  Source: mbdavid/LiteDB

Every operation with collection throw System.InvalidOperationException.

Framework: Xamarin.Forms
Platform: Android OS
Library Version: 5.0.0-alpha

Code to reproduce:

var db = new LiteDatabase("database.db"));
var collection = db.GetCollection<T>("exampleColleciton");
var allElements= collection.FindAll();

Also Insert, Delete, Find will throw
System.InvalidOperationException: Sequence contains no elements
StackTrace:

Enumerable.First[TSource] (System.Collections.Generic.IEnumerable`1[T] source)
LiteDB.Engine.LiteEngine
LiteDB.Engine.LiteEngine..ctor (LiteDB.Engine.EngineSettings settings) [0x0011b] in <c593d84b647a4ed5b475ff841900e3c2>:0
LiteDB
ConnectionString.CreateEngine ()
System
Lazy`1[T].ViaFactory (System.Threading.LazyThreadSafetyMode mode)
System
LazyHelper.ThrowException ()
System
Lazy`1[T].CreateValue ()
System
Lazy`1[T].get_Value ()
System
Lazy`1[T].CreateValue ()
System
Lazy`1[T].get_Value ()
LiteDB
LiteCollection`1[T].Find (LiteDB.Query query, System.Int32 skip, System.Int32 limit)
need review

Most helpful comment

I ran into the same problem. It only occurs when I create the database completely new (0kB file). The Exception is thrown on LiteDatabase db = new LiteDatabase(liteDbPath). It can however be opened by the LiteDB Studio.
I solved the problem by simply downgrading to 4.1.4.

All 8 comments

I ran into the same problem. It only occurs when I create the database completely new (0kB file). The Exception is thrown on LiteDatabase db = new LiteDatabase(liteDbPath). It can however be opened by the LiteDB Studio.
I solved the problem by simply downgrading to 4.1.4.

Yeah I've got the same problem with Xamarin.Forms 4.5.
The App is crashing at this Line:

var db = new LiteDatabase(dbPath);

I switched back to 4.1.4.

Also 5.0.7

@VasenevEA @Enviolet @bluekuen @biapar This error is probably happening because LiteDB is trying to open a zero-byte file (which is obviously not a valid datafile). We'll release a fix for this eventually, but in the meantime just make sure the file you're trying to open is a valid datafile (and not a zero-byte file).

yeah that was the problem, i fixed it by deleting the file creation:

        public string GetDatabasePath()
        {
            var path = Path.Combine(_appData, "ZAsetMobile.db");

            //if (!File.Exists(path))
            //{
            //    using (var streamWriter = new StreamWriter(File.Create(path))) ;
            //}

            return path;
        }

Many thanks for your help!
kind regards, bluekuen

@VasenevEA @Enviolet @bluekuen @biapar This error is probably happening because LiteDB is trying to open a zero-byte file (which is obviously not a valid datafile). We'll release a fix for this eventually, but in the meantime just make sure the file you're trying to open is a valid datafile (and not a zero-byte file).

"Fix this eventually"? I just came across this problem and it seems like it should be a relatively simply fix to check for a zero byte file and act accordingly. It's something people are quite likely to do when creating a new DB so I think you guys should fix it ASAP.

Is this fixed already?

2020-10-26
v 5.0.9
Not fixed yet.

Windows 10 x64
Net Core 3.1

using LiteDB;
using System.IO;

namespace LiteDBTest
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var connectionString = new ConnectionString
            {
                Filename = Path.GetTempFileName(), // Create temporary zero-length file.
            };

            // File.Delete(connectionString.Filename); // Workaround.

            try
            {
                using (var db = new LiteDatabase(connectionString)) // System.InvalidOperationException: "Sequence contains no elements".
                {
                    var names = db.GetCollectionNames();
                }
            }
            finally
            {
                File.Delete(connectionString.Filename);
            }
        }
    }
}

Stack trace:

at System.Linq.ThrowHelper.ThrowNoElementsException()
at System.Linq.Enumerable.FirstTSource
at LiteDB.Engine.LiteEngine..ctor(EngineSettings settings)
at LiteDB.ConnectionString.CreateEngine()
at LiteDB.LiteDatabase..ctor(ConnectionString connectionString, BsonMapper mapper)
at LiteDBTest.Program.Main(String[] args) in C:\LiteDBTest\Program.cs:line 21

Was this page helpful?
0 / 5 - 0 ratings