question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fetching a Nullable<Enum> value.

See original GitHub issue

I would like to retrieve an already persisted enum value from the database. Is there a problem with the following test code?

using System;
using System.IO;

using NUnit.Framework;

using SQLite;

namespace Tests
{
    enum Foo
    {
        Bar = 1,
        Baz = 2
    }

    class FooEntity
    {
        [PrimaryKey, AutoIncrement]
        public int? ID { get; set; }
        public Foo? FooEnum { get; set; }
    }

    [TestFixture]
    public class NullableEnumFetchingTests
    {
        [Test]
        public void NullableEnumStorageTest ()
        {
            SQLiteDatabase db = new SQLiteDatabase (DatabaseFilePath ("NullableEnumStorageTests.db3"));
            db.CreateTable<FooEntity> ();

            FooEntity e1 = new FooEntity {
                FooEnum = Foo.Baz
            };

            db.Insert (e1);

            FooEntity e2 = db.Get<FooEntity> (e1.ID);
            Assert.AreEqual (e1.FooEnum, e2.FooEnum);
        }

        public static string DatabaseFilePath (string sqliteFilename)
        {
            if (sqliteFilename == null)
                throw new ArgumentNullException ("sqliteFilename"); 
            //
            // Source: http://docs.xamarin.com/ios/Guides/Application_Fundamentals/Working_with_the_File_System
            // We need to put in /Library/ on iOS5.1 to meet Apple's iCloud terms
            // (they don't want non-user-generated data in Documents)
            string documentsPath = Environment.GetFolderPath (Environment.SpecialFolder.Personal); // Documents folder
            string libraryPath = Path.Combine (documentsPath, "../Library/"); // Library folder
            var path = Path.Combine (libraryPath, sqliteFilename);

            return path;    
        }
    }
}

Right after inserting an instance of my FooEntity into the database I was able to take the next screenshot.

FooEntityRowPNG

But when trying to retrieve the inserted instance back, I am facing the next exception:

[FAIL] NullableEnumStorageTest : System.ArgumentException : failed to convert parameters
2013-01-30 18:41:28.979 Tests[54480:c07]          at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x000eb] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:242 
2013-01-30 18:41:28.980 Tests[54480:c07]          at System.Reflection.MonoProperty.SetValue (System.Object obj, System.Object value, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] index, System.Globalization.CultureInfo culture) [0x00064] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/MonoProperty.cs:353 
2013-01-30 18:41:28.981 Tests[54480:c07]          at System.Reflection.PropertyInfo.SetValue (System.Object obj, System.Object value, System.Object[] index) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/PropertyInfo.cs:93 
2013-01-30 18:41:28.981 Tests[54480:c07]          at SQLite.TableMapping+Column.SetValue (System.Object obj, System.Object val) [0x00000] in /Users/SQLite.cs:1609 
2013-01-30 18:41:28.982 Tests[54480:c07]          at SQLite.SQLiteCommand+<ExecuteDeferredQuery>c__Iterator0`1[A.a.Tests.DAL.FooEntity].MoveNext () [0x0016f] in /Users/SQLite.cs:1820 
2013-01-30 18:41:28.982 Tests[54480:c07]          at System.Collections.Generic.List`1[A.a.Tests.DAL.FooEntity].AddEnumerable (IEnumerable`1 enumerable) [0x0001a] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Collections.Generic/List.cs:127 
2013-01-30 18:41:28.983 Tests[54480:c07]          at System.Collections.Generic.List`1[A.a.Tests.DAL.FooEntity]..ctor (IEnumerable`1 collection) [0x0002f] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Collections.Generic/List.cs:64 
2013-01-30 18:41:28.983 Tests[54480:c07]          at System.Linq.Enumerable.ToList[FooEntity] (IEnumerable`1 source) [0x00006] in /Developer/MonoTouch/Source/mono/mcs/class/System.Core/System.Linq/Enumerable.cs:2931 
2013-01-30 18:41:28.984 Tests[54480:c07]          at SQLite.SQLiteCommand.ExecuteQuery[FooEntity] () [0x00000] in /Users/SQLite.cs:1773 
2013-01-30 18:41:28.984 Tests[54480:c07]          at SQLite.SQLiteConnection.Query[FooEntity] (System.String query, System.Object[] args) [0x00009] in /Users/SQLite.cs:531 
2013-01-30 18:41:28.985 Tests[54480:c07]          at SQLite.SQLiteConnection.Get[FooEntity] (System.Object pk) [0x00011] in /Users/SQLite.cs:638 
2013-01-30 18:41:28.986 Tests[54480:c07]          at A.a.Tests.DAL.NullableEnumFetchingTests.NullableEnumStorageTest () [0x00034] in /Users/NullableEnumFetchingTests.cs:44 
2013-01-30 18:41:28.986 Tests[54480:c07]          at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
2013-01-30 18:41:28.987 Tests[54480:c07]          at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x000c0] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:228 

I have tried to fix this issue by following the next link: http://code.google.com/p/sqlite-net/issues/detail?id=47

But it isn’t compatible with silverlight and monotouch.

May some of you folks help me with this issue? Thanks in advance.

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Comments:15 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
fedemkrcommented, Apr 27, 2017

As a workaround, we could just ignore the nullable enum and add another property that wraps the nullable one as a short or whatever type the enum can be casted to:

        [Ignore]
        public Priority? Priority { get; set; }

        public short PrioritySQL
        {
            get
            {
                return (short?)this.Priority ?? -1;
            }
            set
            {
                this.Priority = value == -1 ? (Priority?)null : (Priority)value;
            }
        }

Hope this helps!

2reactions
chernihivcommented, Feb 13, 2017

Hi guys, the issue had been created 3-4 years ago but I found this bug yesterday. Has it been fixed?

I’ve got entity with ‘ChecklistResultStatus?’ prop. There are a lot of data within table with real values (ChecklistResultStatus: 1,2,3,4, ect), but I can’t load it. Additional information: Object of type 'System.Int32' cannot be converted to type 'System.Nullable1[Enums.ChecklistResultStatus]'.

Is there any estimates when this bug is going to be fixed?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Passing null when can't fetch an enum value?
As a rule, I prefer to not pass null around and instead use a special value like an empty List or a special...
Read more >
Nullable enumeration values vs. "NoValue" or "Undefined ...
My question is: is it better to represent the value of the Result field in my domain object as a nullable value (e.g....
Read more >
C# Enums – Handling NULL Values – Some Tips
This approach would solve your the problem of checking for 'null' value all over the project and you can simply use a switch()...
Read more >
Solved - A null on my enum
So I have an enumeration and a method to go through that enumeration and find the enum.name() based off its first given value....
Read more >
MySQL 8.0 Reference Manual :: 11.3.5 The ENUM Type
An ENUM is a string object with a value chosen from a list of permitted values that are enumerated explicitly in the column...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found