使用SQLiteAsyncConnection抛出MissingMethodException

时间:2016-02-27 11:18:01

标签: c# sqlite win-universal-app portable-class-library sqlite-net

首次使用SQLite,选择SQLite.net-pcl

我有一个PCL库,我从我的UWP应用中消费,我的PCL库有DBContext类:

public NewspaperDataContext(ISQLitePlatform sqLitePlatform, string applicationPath, string dataBaseName)
{
     _sqlConnection = new SQLiteAsyncConnection(()=> new SQLiteConnectionWithLock(sqLitePlatform, 
     new SQLite.Net.SQLiteConnectionString(Path.Combine(applicationPath, dataBaseName), storeDateTimeAsTicks: false)));

     CreateTableAsync(_sqlConnection, typeof(Article)).Result.Wait();
     CreateTableAsync(_sqlConnection, typeof(Author)).Result.Wait();
     CreateTableAsync(_sqlConnection, typeof(Category)).Result.Wait();
     CreateTableAsync(_sqlConnection, typeof(Group)).Result.Wait();

     var c = _sqlConnection.Table<Article>();
}

private async Task<Task> CreateTableAsync(SQLiteAsyncConnection asyncConnection, Type table)
{
     return asyncConnection.CreateTableAsync<Author>().ContinueWith((results) =>
     {
           Debug.WriteLine(!results.IsFaulted
           ? $"Error where creating the {nameof(table)} table !!"
           : $"Table {nameof(table)} created sucessfully!");
     });
}

我从我的UWP应用程序中调用NewspaperDataContext,如下所示:

private async void MainPage_OnLoaded(object sender, RoutedEventArgs e)
{            
    var n = new NewspaperDataContext(new SQLitePlatformWinRT(), ApplicationData.Current.LocalFolder.Path, "LiberteDB");
}

在以下行的NewspaperDataContext构造函数中:

var c = _sqlConnection.Table<Article>();

抛出一个奇怪的MissingMethodException:

  

类型&#39; System.MissingMethodException&#39;的例外情况发生在   SQLite.Net.Async.dll但未在用户代码中处理

     

其他信息:找不到方法:&#39;无效   SQLite.Net.SQLiteConnectionString..ctor(System.String,Boolean,   SQLite.Net.IBlobSerializer,SQLite.Net.IContractResolver)&#39;。

无法在互联网上找到与此错误相关的任何内容,请有人帮忙。

1 个答案:

答案 0 :(得分:0)

sqlite-net-pcl仅应添加到PCL项目。如果将其添加到其他任何平台(例如Android),它将得到此MethodMissingException。因此,请从其他平台删除该软件包,因为仅PCL项目需要此软件包。我遇到相同问题并找到解决方案时,给出了此答案。在使用Xamarin.Forms开发应用程序时遇到了此异常。