title | description | ms.date |
---|---|---|
.NET Aspire Community Toolkit SQLite Entity Framework integration |
Learn how to use the .NET Aspire SQLite Entity Framework integration for efficient data management within your applications. |
03/04/2025 |
[!INCLUDE includes-hosting-and-client]
[!INCLUDE banner]
SQLite is a lightweight, serverless, self-contained SQL database engine that is widely used for local data storage in applications. The .NET Aspire SQLite integration provides a way to use SQLite databases within your .NET Aspire applications, and access them via the Microsoft.EntityFrameworkCore.Sqlite
Entity Framework support package.
[!INCLUDE sqlite-hosting]
To get started with the .NET Aspire SQLite EF client integration, install the 📦 CommunityToolkit.Aspire.Microsoft.EntityFrameworkCore.Sqlite NuGet package in the client-consuming project, that is, the project for the application that uses the SQLite client. The SQLite client integration registers a SqliteConnection
instance that you can use to interact with SQLite.
dotnet add package CommunityToolkit.Aspire.Microsoft.EntityFrameworkCore.Sqlite
<PackageReference Include="CommunityToolkit.Aspire.Microsoft.EntityFrameworkCore.Sqlite" Version="*" />
In the :::no-loc text="Program.cs"::: file of your client-consuming project, call the Microsoft.Extensions.Hosting.AspireEFSqliteExtensions.AddSqliteDbContext
extension method on any xref:Microsoft.Extensions.Hosting.IHostApplicationBuilder to register your xref:System.Data.Entity.DbContext subclass for use via the dependency injection container. The method takes a connection name parameter.
builder.AddSqliteDbContext<YourDbContext>(connectionName: "sqlite");
Tip
The connectionName
parameter must match the name used when adding the SQLite resource in the app host project. For more information, see Add SQLite resource.
After adding YourDbContext
to the builder, you can get the YourDbContext
instance using dependency injection. For example, to retrieve your data source object from an example service define it as a constructor parameter and ensure the ExampleService
class is registered with the dependency injection container:
public class ExampleService(YourDbContext context)
{
// Use context...
}
For more information on dependency injection, see .NET dependency injection.
You may prefer to use the standard Entity Framework method to obtain the database contextand add it to the dependency injection container:
builder.Services.AddDbContext<YourDbContext>(options =>
options.UseSqlite(builder.Configuration.GetConnectionString("sqlite")
?? throw new InvalidOperationException("Connection string 'sqlite' not found.")));
Note
The connection string name that you pass to the xref:Microsoft.Extensions.Configuration.ConfigurationExtensions.GetConnectionString* method must match the name used when adding the SQLite resource in the app host project. For more information, see Add SQLite resource.
The SQLite client integration provides multiple configuration approaches and options to meet the requirements and conventions of your project.
When using a connection string from the ConnectionStrings
configuration section, you can provide the name of the connection string when calling the Microsoft.Extensions.Hosting.AspireEFSqliteExtensions.AddSqliteDbContext
method:
builder.AddSqliteDbContext<YourDbContext>("sqlite");
Then the connection string will be retrieved from the ConnectionStrings
configuration section.
{
"ConnectionStrings": {
"sqlite": "Data Source=C:\\Database\\Location\\my-database.db"
}
}
The SQLite client integration supports xref:Microsoft.Extensions.Configuration?displayProperty=fullName. It loads the Microsoft.Extensions.Hosting.SqliteConnectionSettings
from the :::no-loc text="appsettings.json"::: or other configuration providers by using the Aspire:Sqlite:EntityFrameworkCore:Sqlite
key. Example _:::no-loc text="appsettings.json"::: that configures some of the options:
{
"Aspire": {
"Sqlite": {
"EntityFrameworkCore": {
"Sqlite": {
"ConnectionString": "Data Source=C:\\Database\\Location\\my-database.db",
"DisableHealthCheck": true
}
}
}
}
}