0

I get this error when I am makeing a SQKuteAsyncConnection:

**System.TypeInitializationException:** 'The type initializer for 'SQLite.SQLiteConnection' threw an exception.'

My class looks like this:

    internal class DbContext : IDbContext
    {
        private SQLiteAsyncConnection database;

        public DbContext()
        {
            SetupDatabase();
        }

        public async void SetupDatabase()
        {
            database = new SQLiteAsyncConnection(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "mydatabase.db3"));
            await database.CreateTableAsync<Token>();
        }
    }

Here is the debugger when the database has been set with the SQLiteAsyncConnection> enter image description here

Here are my Nuget packages:

    <ItemGroup>
      <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.1" />
      <PackageReference Include="Microsoft.Maui.Controls.Maps" Version="7.0.52" />
      <PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
      <PackageReference Include="sqlite-net-pcl" Version="1.8.116" />
      <PackageReference Include="SQLitePCLRaw.bundle_green" Version="2.1.3" />
      <PackageReference Include="SQLitePCLRaw.core" Version="2.1.3" />
      <PackageReference Include="SQLitePCLRaw.provider.dynamic_cdecl" Version="2.1.3" />
      <PackageReference Include="SQLitePCLRaw.provider.sqlite3" Version="2.1.3" />
    </ItemGroup>

I have used this repo to try and understand the code: https://github.com/mistrypragnesh40/SQLiteDemoWithBlazorApp

UPDATE

After further testing it works on "Windows machine" debugger in visual studio but not on physical device (neither Andorid Emulator). I have a Samsung Note running latest Android. Any clues?

Doesnt work on Android Emulator either, same error:

System.DllNotFoundException: 'e_sqlite3'

and

System.Exception: You need to call SQLitePCL.raw.SetProvider().  If you are using a bundle package, this is done by ca…
Hickori
  • 55
  • 6
  • Have you tried on other devices? I tested on my android emulator, but it works on my side. I could insert an item and get the inserted item correctly. – Jessie Zhang -MSFT Jan 03 '23 at 00:36

2 Answers2

1

Got it working!

Solution: Clean & Build

Hickori
  • 55
  • 6
0

One think I noticed in your code. SetupDatabase is an async void, however that cannot be awaited and should not be used like that, as mentioned here for example: Why exactly is void async bad?

It is difficult to determine the exact cause, but this is definitely a starting point.

Jan Skála
  • 374
  • 4
  • 13
  • Okay, I will try it later and tell you if it works any better, but should the path in the SQLiteAsyncConnection work on Android? Or should it be somewhat different? Because it seems to be working on Windows Machine – Hickori Dec 30 '22 at 13:08
  • long time since I have done android, but the file system is very restrictive I have found a similar problem with xamarin, and this sovled it https://stackoverflow.com/a/39896490/4438653 – Jan Skála Dec 30 '22 at 13:59
  • Tried it but the same error, Ive updated the main post with some exceptions and my Nuget list. – Hickori Dec 31 '22 at 14:27