skip to Main Content

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…

2

Answers


  1. Chosen as BEST ANSWER

    Got it working!

    Solution: Clean & Build


  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search