简体   繁体   中英

.NET Maui SQLite Connection

I have added the sqlite-net-plc package to my project and created a Database class like this:

public class Database
{
    public Database()
    {
        var dbPath = Path.Combine(FileSystem.AppDataDirectory, "databaseTest.db3");

        Connection = new SQLiteAsyncConnection(dbPath);
        Connection.CreateTableAsync<User>().Wait();
    }

    public SQLiteAsyncConnection Connection { get; set; }
}

This seems to work fine with Android applications allowing me to manipulate the database from anywhere.

However, when I run this application through an IOS simulator with an active Mac connection I get the following error:

The type initializer for 'SQLite.SQLiteConnection' threw an exception.

I believe that an active connection to a SQLite database cannot be resolved but I don't know why. Any insight on this would be great.

Thanks in advance.

I was seeing the same exception on both iOS and Android after adding sqlite-net-pcl to the stock MAUI test app until I explicitly added the https://www.nuget.org/packages/SQLitePCLRaw.bundle_green/2.0.7 package to the project.

I see that on September 7 the "raw" package author noted that he had work to do: https://github.com/ericsink/SQLitePCL.raw/discussions/449#discussioncomment-1282643

sqlite-net-pcl depends on version 2.0.4 of the "raw" nuget which is over a year old. Maybe 2.0.7 fixed something relevant for iOS? I see some Apple-specific commits at https://github.com/ericsink/SQLitePCL.raw/commits/master in that timeframe.

Maybe you already had a newer version installed that had fixes for Android, but not iOS?

问题在 github 上回答: https : //github.com/dotnet/maui/issues/3211

For me to get iOS to work:

Install these packages

<PackageReference Include="sqlite-net-pcl" Version="1.8.116" />
<PackageReference Include="SQLitePCLRaw.provider.sqlite3" Version="2.1.0" />
<PackageReference Include="SQLitePCLRaw.bundle_green" Version="2.1.0" />

Then change Platforms.iOS.AppDelegate.cs to

[Register("AppDelegate")]
public class AppDelegate : MauiUIApplicationDelegate
{
    protected override MauiApp CreateMauiApp()
    {
        raw.SetProvider(new SQLite3Provider_sqlite3());
        return MauiProgram.CreateMauiApp();
    }
}

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