简体   繁体   中英

Xamarin Forms UWP Sqlite connection, Could not load file or assembly SQLite-net

I am stuck in connecting sqlite with xamarin forms/pcl/uwp application. Following is my code to set path for database

Android (Following works perfectly alright)

App1.globals.path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
//App1.globals.path=> /data/user/0/com.companyname.App1/files
LoadApplication(new App1.App());

UWP (Following does not work)

App1.globals.path = Windows.Storage.ApplicationData.Current.LocalFolder.Path;
//App1.globals.path => C:\Users\sami.akram\AppData\Local\Packages\8e3de984-9360-4549-a5cc-80071995402b_hy7qfqqm9rwga\LocalState
LoadApplication(new App1.App());

Following is the code in App1 (Portable) => Main App

[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
namespace App1
{

    public class globals
    {
        public static string path = "test.db3";
    }

    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();
            var dbPath = System.IO.Path.Combine(globals.path, "test.db3");
            var db = new SQLiteConnection(dbPath);
            SetMainPage();
        }
    }
}

Error I face is

Could not load file or assembly 'SQLite-net, Version=1.4.118.0, Culture=neutral, PublicKeyToken=null'. The located assembly's manifest definition does not match the assembly reference

在此处输入图片说明

But assembly version should not be any issue as I have installed same sqllite for all projects following image shows it. Note I tried the app with .net 4.5 and .net 4.6 but it results same.

在此处输入图片说明

I tried clean solution rebuild solution. Nothing is helping yet, same error could not load assembly..

You might also need to ensure you have the VS extension provided by Sqlite.org installed.

Basically, you need to enable SQLite on UWP Apps. The SQLite core engine is already included on iOS and Android, but not on Windows. Therefore, you wil need to install the SQLite extension for Visual Studio, which provides precompiled binaries for the database engine and automates the task of including the required files with new projects.

A detailed article with steps on how to integrate Sqlite package in Xamarin.Forms project for UWP can be found here

There's an issue with the "sqlite-net-pcl" NuGet package version 1.4.118.0 that only affects UWP projects referencing a Xamarin.Forms PCL project.
The bug has been fixed with the "sqlite-net-pcl" version 1.5.166-beta.

A couple of issues have been added to the official SQLite-net GitHub page:

you have to use platform-specific Windows.Storage API to determine the database file path. You can use following link for help

https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/databases/#Windows_10_Universal_Windows_Platform_UWP

Here is the sample code:

https://developer.xamarin.com/samples/xamarin-forms/Todo/

I have tested your code and reproduced your issue. The problem is that you have no permission to access windows local folder in portable library with Version=4.0.10.0 System.IO .

portable library System.IO version

便携式库System.IO版本 UWP client project System.IO version

UWP客户端项目System.IO版本

The way to use sqlite within xamarin form depends on dependency service. you could refer to the following.

public interface IFileHelper
{
  string GetLocalFilePath(string filename);
}

Interface implementation

using Windows.Storage;
...

[assembly: Dependency(typeof(FileHelper))]
namespace Todo.UWP
{
    public class FileHelper : IFileHelper
    {
        public string GetLocalFilePath(string filename)
        {
            return Path.Combine(ApplicationData.Current.LocalFolder.Path, filename);
        }
    }
}

usage

var path = DependencyService.Get<IFileHelper>().GetLocalFilePath("TodoSQLite.db3")
var db = new SQLiteConnection(path);

For more please refer to Local Databases .

It seems that this is a particular issue of sqlite-net. Even with the implementation of xamarin's page ( See xamarin's implementation ) it shows that error.

What worked for me was to update the sqlite-net-pcl to 1.5.166-beta.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM