How to load extension for SQLite (SQLite + Entity Framework Core)

76 views Asked by At

I am using SQLite with Entity Framework Core in my .NET project. But I want to add some extensions for SQLite. How to correctly load SQLite extension? Nuget package does not exists, only .dll SQLite extension.

I found this article Loading SQLite Extensions, but there using Microsoft.Data.Sqlite.SqliteConnection, and I don't understand, how I may config DbContext service in Startup class or something else.

1

There are 1 answers

0
Paul On

I create IDbConnectionInterceptor

public class SQLiteExtensionInterceptor : IDbConnectionInterceptor
{
    public DbConnection ConnectionCreated(ConnectionCreatedEventData eventData,
        DbConnection result)
    {
        var sqliteConnection = (SqliteConnection)result;
        sqliteConnection.EnableExtensions();
        sqliteConnection.LoadExtension(@"path to extension");
        return sqliteConnection;
    }
}

and in Startup.cs ConfigureServices() add DB service

services.AddDbContext<YourDbContext>(options =>
{
    options.UseSqlite("connectionString");
    options.AddInterceptors(new SQLiteExtensionInterceptor());
});

Thanks for Svyatoslav Danyliv.