Entity Framework Entity Framework with SQLite Setting up a project to use Entity Framework with an SQLite provider

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

The Entity Framework library comes only with an SQL Server provider. To use SQLite will require additional dependencies and configuration. All required dependencies are available on NuGet.

Install SQLite Managed Libraries

All of the mananged depedencies can be installed using the NuGet Package Manager Console. Run the command Install-Package System.Data.SQLite.

Installing System.Data.SQLite

As shown above, when installing System.Data.SQLite, all related managed libraries are installed with it. This includes System.Data.SQLite.EF6, the EF provider for SQLite. The project also now references the assemblies required to use the SQLite provider.

Project references

Including Unmanaged Library

The SQLite managed libraries are dependent on an unmanaged assembly named SQLite.Interop.dll. It is included with the package assemblies downloaded with the SQLite package, and they are automatically copied into your build directory when you build the project. However, because it's unmanaged, it will not be included in your reference list. But make note, this assembly most be distributed with the application for the SQLite assemblies to work.

Note: This assembly is bit-dependent, meaning you will need to include a specific assembly for each bitness you plan to support (x86/x64).

Editing the project's App.config

The app.config file will require some modifications before SQLite can be used as an Entity Framework provider.

Required Fixes

When installing the package, the app.config file is automatically updated to include the necessary entries for SQLite and SQLite EF. Unfortunately these entries contain some errors. They need to be modified before it will work correctly.

First, locate the DbProviderFactorieselement in the config file. It is within the system.data element and will contain the following

<DbProviderFactories>
  <remove invariant="System.Data.SQLite.EF6" />
  <add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".NET Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />
  <remove invariant="System.Data.SQLite" /><add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".NET Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
</DbProviderFactories>

This can be simplified to contain a single entry

<DbProviderFactories>
    <add name="SQLite Data Provider" invariant="System.Data.SQLite.EF6" description=".NET Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
</DbProviderFactories>

With this, we have specified the EF6 SQLite providers should use the SQLite factory.

Add SQLite connection string

Connection strings can be added to the configuration file within the root element. Add a connection string for accessing an SQLite database.

<connectionStrings>
    <add name="TestContext" connectionString="data source=testdb.sqlite;initial catalog=Test;App=EntityFramework;" providerName="System.Data.SQLite.EF6"/>
</connectionStrings>

The important thing to note here is the provider. It has been set to System.Data.SQLite.EF6. This tells EF that when we use this connection string, we want to use SQLite. The data source specified is just an example and will be dependent on the location and name of your SQLite database.

Your first SQLite DbContext

With all the installation and configuration complete, you can now start using a DbContext that will work on your SQLite database.

public class TestContext : DbContext
{
    public TestContext()
        : base("name=TestContext") { }
}

By specifying name=TestContext, I have indicating that the TestContext connection string located in the app.configfile should be used to create the context. That connection string was configured to use SQLite, so this context will use an SQLite database.



Got any Entity Framework Question?