mercredi 6 janvier 2016

C# UWP SQlite.Net.PCL Constructor on type not found, System.MissingMethodException occurred in mscorlib.ni.dll

I created a UWP app to work on SQLite, thus added the below references to the project

1) SQLite VSIX package for Universal App Platform development using Visual Studio 2015 from http://ift.tt/UeQUv3

2) Visual C++ 2015 Runtime for Universal Windows version 14:0 to avoid a VS warning due to step 1)

And chose the wrapper (fork of the original sqlite.net wrapper)

3) SQLite.Net.PCL (http://ift.tt/1PNMmev version 3.1.1

All code is on MainPage.xaml.cs, one works and the other one does not.

Works

using SQLite.Net;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Windows.UI.Xaml.Controls;

namespace SQLite_v3
{

    public sealed partial class MainPage : Page
    {
        private string path;
        public MainPage()
        {
            this.InitializeComponent();
            this.path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db.sqlite");

            //Create Table and Insert item
            using (var db = new SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path))
            {
                db.CreateTable<A>();
                db.Insert(new A() { Symbol="Hello"}); //Will be changed below
            };
            //Query and output
            using (var db = new SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path))
            {
                foreach (var item in db.Table<A>().Select(v => v))
                    Debug.WriteLine("A: " + item.Symbol);
            };
        }
    }
    public class A
    {
        public int Id { get; set; }
        public string Symbol { get; set; }
    }
}

Does not work (even by deleting manually the database in the packaging folder/LocalState before running the new code)

In this scenario I naively changed class A to include a constructor with a parameter for future inclusion in a larger program. This is where I get the bug. The following non-working code differs from the previous one in the

-ctor in class A

-Insert method which now calls the new A ctor

using SQLite.Net;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Windows.UI.Xaml.Controls;

namespace SQLite_v3
{

    public sealed partial class MainPage : Page
    {
        private string path;
        public MainPage()
        {
            this.InitializeComponent();
            this.path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db.sqlite");

            //Create Table and Insert item
            using (var db = new SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path))
            {
                db.CreateTable<A>();
                db.Insert(new A("Hello")); //Changed with respect to the working scenario
            };
            //Query and output
            using (var db = new SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path))
            {
                foreach (var item in db.Table<A>().Select(v => v))
                    Debug.WriteLine("A: " + item.Symbol);
            };
        }
    }
    public class A
    {
        public int Id { get; set; }
        public string Symbol { get; set; }
        public A(string symbol) //Change with respect to the working scenario
        {
            Symbol = symbol;
        }
    }
}

Exception

The code builds without complaining, but when I run it I get an exception in the foreach loop:

An exception of type 'System.MissingMethodException' occurred in mscorlib.ni.dll but was not handled in user code

Additional information: Constructor on type 'SQLite_v3.A' not found.

Solution

The only solution I found was to add the default ctor in class A in addition to the parameterized one, but I would like to avoid that and above all I do not grasp why I lost this possibility.

Questions

-Is it possible to avoid the default ctor in the definition of class A; and if not why?

-Any idea on why this is happening?

Thanks very much for your help.

Aucun commentaire:

Enregistrer un commentaire