liveBook · Manning

SqliteConnection

As explained in ASP.NET Core in Action, 3e

SqliteConnection is the Microsoft.Data.Sqlite connection type used to connect an application to a SQLite database.

SqliteConnection is the Microsoft.Data.Sqlite connection type used to create an isolated SQLite database for EF Core tests. Construct it with "DataSource=:memory:" and open it before creating the DbContext. section 36.4

Use the connection in this sequence:

  • Create a SqliteConnection with the "DataSource=:memory:" connection string. section 36.4
  • Call Open() yourself so the database remains available while multiple DbContext instances use it. section 36.5
  • Pass the open connection to DbContextOptionsBuilder<AppDbContext>.UseSqlite(). section 36.4
  • Pass the resulting options to AppDbContext and call EnsureCreated() before using the test data. section 36.5

The database exists only while the connection stays open. Closing or disposing the connection destroys the in-memory database, while manually opening it allows several DbContexts to share the same isolated test database. section 36.4 section 36.5

Testing with an in-memory SQLite connection

var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); var options = new DbContextOptionsBuilder<AppDbContext>() .UseSqlite(connection) .Options; using (var context = new AppDbContext(options)) { context.Database.EnsureCreated(); // Add test entities and exercise the service. }

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains construction, opening, EF Core configuration, database creation, and connection lifetime, but not broader connection APIs.
Examples
Demonstrates a RecipeService test that seeds recipes into an in-memory SQLite database and loads them through AppDbContext.
What it assumes
Assumes familiarity with C# tests and EF Core DbContext while showing the SQLite provider setup explicitly.
What you can do after
Build it from scratch for isolated EF Core tests using an explicitly opened SQLite connection and configured DbContext options.
How it connects
Connects SqliteConnection with EF Core, DbContext, DbContextOptionsBuilder, UseSqlite, and SQLite in-memory databases.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.