With recent updates of the mono runtime, and the reactivity of the .NET team to fix issues, it’s now possible to use SQLite with the Uno WebAssembly Bootstrapper and Uno Platform apps.This support is now enabled through WebAssembly static linking, and the reuse of LLVM bitcode compiled source, such as SkiaSharp or SQLite. It allows for an easy use of code written in other languages, from an app built with C#.
With the Erik Sink’s recent updates to SQLitePCLRaw 2.0, it’s now possible with .NET for WebAssembly to use the existing official packages. This also enables Entity Framework Core 3.1, and Frank Krueger’s SQLite-net to work out of the box without any specific configuration for WebAssembly.
About Uno Platform
For those new to Uno Platform – it enables for creation of single-source C# and XAML apps which run natively on iOS and Android, macOS and Web via WebAssembly. Uno Platform is Open Source (Apache 2.0) and available on GitHub. To learn more about Uno Platform, see how it works, or create a small sample app.
Client-side SQLite and Entity Framework Core
In order to demonstrate the use of client-side only technologies in the browser, we’ve updated our SQLite 3.3 + EFCore 3.1 + Roslyn 3.6 sample. EF Core’s support for SQLite relies on SQLitePCLRaw, making it instantly compatible with WebAssembly.
This sample takes some C# code, compiles it to an in-memory assembly using the Roslyn, the runs it in the browser. You can run the live example here: https://sqliteefcore-wasm.platform.uno/
Getting started with SQLite, WebAssembly and Uno Platform
It’s very simple to start with SQLite on WebAssembly using the Uno Platform.
- Create an Uno Application using our VS Templates or dotnew new templates
- Make sure to update Uno.Wasm.Bootstrap and Uno.Wasm.Bootstrap.DevServer packages to the 1.3 experimental (ending in -dev.xx) or stable.
- Add a reference to the sqlite-net-pcl nuget package
- Add a reference to the Uno.SQLitePCLRaw.provider.wasm package (3.0 experimental or stable)
- Follow the instructions of the SQLite-net repository.
You can find a full example here: https://github.com/unoplatform/Uno.Samples/tree/master/UI/SQLiteSample
Under the hood
Uno Platform has had support for SQLite for a long while now through dynamic linking. However, for many developers dynamic linking support had not been a practical solution and it had been difficult to work with. We chose to move to enable the use of static linking and WSL builds, allowing an easier use of the SQLite library compiled to WebAssembly.
SQLite access in .NET is generally done through SQLitePCLRaw. It provides a standard package that uses P/Invoke to the e_sqlite3 native module, and .NET for WebAssembly also supports the use of [DllImport] to interoperate with non-.NET WebAssembly code.
All the operations provided by ISQLite3Provider are enabled using the standard nuget packages, making it effortless for WebAssembly apps to follow the updates made to the official packages.
We will be talking about SQLite and multi-platform development at our annual UnoConf. This year we are hosting it online on August 12. Register now – it is free of charge.
Jerome Laban, on behalf of Uno Platform team