使用 SQLiteAsyncConnection 抛出 MissingMethodException
本文关键字:MissingMethodException 抛出 SQLiteAsyncConnection 使用 | 更新日期: 2023-09-27 18:36:33
第一次使用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,上面写着:
类型为"System.MissingMethodException"的异常发生在 SQLite.Net.Async.dll但未在用户代码中处理
附加信息:未找到方法:"无效 SQLite.Net.SQLiteConnectionString..ctor(System.String, Boolean, SQLite.Net.IBlobSerializer, SQLite.Net.IContractResolver)'.
在互联网上找不到与此错误相关的任何内容,请有人帮忙。
包
sqlite-net-pcl
应仅添加到 PCL 项目中。如果您将其添加到任何其他平台(例如Android),它将提供此MethodMissingException
。因此,请从其他平台中删除该包,因为只有 PCL 项目才需要此包。当我遇到同样的问题并找到解决方案时,我给出了这个答案。我在使用 Xamarin.Forms 开发应用程序时遇到了此异常。