samedi 12 décembre 2015

InvalidOperationException when iterating over LINQ-to-SQL Queryable

I am mapping data from a SQLite database to C# objects using LINQ to SQL. Everything works perfectly when I'm running it inside a Visual Studio project, but for some reason I can't get it to work in Unity.

The very simplified code goes something like this:

using System.Data.Linq;
using Mono.Data.Sqlite;

var connectionString = "Data Source=" + filePath + ";DbLinqProvider=SQLite;Version=3;";
var connection = new SqliteConnection(connectionString);
var context = new DataContext(connection);

foreach (Output output in context.GetTable<Output>())
{
    UnityEngine.Debug.Log(output.ID);
}

But in the head of the foreach an InvalidOperationException is thrown, saying "Operation is not valid due to the current state of the object".

  • I know that the mapping works, because it does in Visual Studio.
  • I know that the database connectivity works, because it does when using "normal" DbCommands.
  • I'm still a bit unsure about the included DLLs, but again, I tested it with exactly those ones (contrary to the "built in" ones) in VS and it worked. In my Assets folder I have System.Data.dll, System.Data.Linq.dll, Mono.Data.Sqlite from the Mono install, and sqlite3.dll and sqlite3.def from the SQLite website.

The Exception goes as follows:

InvalidOperationException: Operation is not valid due to the current state of the object
System.Linq.Enumerable.Single[MemberInfo] (IEnumerable`1 source, System.Func`2 predicate, Fallback fallback)
System.Linq.Enumerable.Single[MemberInfo] (IEnumerable`1 source)
DbLinq.Data.Linq.Mapping.AttributedMetaAssociation..ctor (System.Reflection.MemberInfo member, System.Data.Linq.Mapping.    AssociationAttribute attribute, System.Data.Linq.Mapping.MetaDataMember metaDataMember)
DbLinq.Data.Linq.Mapping.AttributedMetaType+<GetAssociations>c__Iterator5.MoveNext ()
System.Collections.Generic.List`1[System.Data.Linq.Mapping.MetaAssociation].AddEnumerable (IEnumerable`1 enumerable) (at /    Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/List.cs:128)
System.Collections.Generic.List`1[System.Data.Linq.Mapping.MetaAssociation]..ctor (IEnumerable`1 collection) (at /Users/    builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/List.cs:65)
System.Linq.Enumerable.ToList[MetaAssociation] (IEnumerable`1 source)
DbLinq.Data.Linq.Mapping.AttributedMetaType.get_Associations ()
System.Data.Linq.DataContext.SetEntitySetsQueries (System.Object entity)
System.Data.Linq.DataContext._GetOrRegisterEntity (System.Object entity)
System.Data.Linq.DataContext.Register (System.Object entity)
DbLinq.Data.Linq.Sugar.Implementation.QueryRunner.Select[Output] (DbLinq.Data.Linq.Sugar.SelectQuery selectQuery)
DbLinq.Data.Linq.Implementation.QueryProvider`1[Bitcoin.Output].GetEnumerator ()
System.Data.Linq.Table`1[Bitcoin.Output].GetEnumerator ()
DAO.SQLiteDAO.test (System.String filePath) (at Assets/DAO/SQLiteDAO.cs:103)
DAO.SQLiteDAO..ctor (System.String filePath) (at Assets/DAO/SQLiteDAO.cs:25)
SphereSpawner.Start () (at Assets/SphereSpawner.cs:30)

I read everything here that seems similar, but I never actively use Single() on anything, so that cannot be it; and I'm quite sure that the list isn't empty, either. It shouldn't be, at least.

Please help, I'm losing my mind here :P

Aucun commentaire:

Enregistrer un commentaire