Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sequence contains no elements at 5.0.0-alpha #1275

Closed
VasenevEA opened this issue Aug 7, 2019 · 9 comments
Closed

Sequence contains no elements at 5.0.0-alpha #1275

VasenevEA opened this issue Aug 7, 2019 · 9 comments
Assignees

Comments

@VasenevEA
Copy link

VasenevEA commented Aug 7, 2019

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)
@Enviolet
Copy link

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.

@bluekuen
Copy link

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.

@biapar
Copy link

biapar commented May 11, 2020

Also 5.0.7

@lbnascimento
Copy link
Contributor

@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).

@bluekuen
Copy link

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

@jez9999
Copy link

jez9999 commented Jun 6, 2020

@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.

@bluekuen
Copy link

bluekuen commented Jul 2, 2020

Is this fixed already?

@riuson
Copy link

riuson commented Oct 26, 2020

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.First[TSource](IEnumerable`1 source)
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

@jez9999
Copy link

jez9999 commented May 4, 2021

This has apparently been fixed, and the bug should probably be closed; see: #1956

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants