Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Sep 5, 2025

This PR implements a new EnrichSqliteDatabaseDbContext<TDbContext> extension method that provides a simplified API for registering Entity Framework Core DbContext with SQLite provider on WebApplicationBuilder, including built-in OpenTelemetry instrumentation support.

API

public static WebApplicationBuilder EnrichSqliteDatabaseDbContext<TDbContext>(
    this WebApplicationBuilder builder,
    string? connectionStringName = "DefaultConnection",
    bool enableOpenTelemetry = true)
    where TDbContext : DbContext

Key Features

  • Simplified API: Works directly with WebApplicationBuilder instead of requiring IHostApplicationBuilder
  • Smart defaults: Uses "DefaultConnection" as the default connection string name
  • Built-in observability: Automatically enables OpenTelemetry EF Core instrumentation by default
  • Developer-friendly: Reduces boilerplate code for common SQLite + EF Core setup scenarios

Usage Examples

// Basic usage with defaults
builder.EnrichSqliteDatabaseDbContext<BloggingContext>();

// Custom connection string name
builder.EnrichSqliteDatabaseDbContext<BloggingContext>("MyDatabase");

// Disable OpenTelemetry if needed
builder.EnrichSqliteDatabaseDbContext<BloggingContext>(enableOpenTelemetry: false);

Benefits

  • Consistency: Aligns SQLite developer experience with other EF Core providers that have similar "Enrich" methods
  • Reduced boilerplate: Eliminates repetitive connection string + DbContext registration code
  • Faster onboarding: Makes it easier for new developers to configure SQLite with minimal ceremony
  • Out-of-the-box observability: Leverages .NET Aspire's telemetry pipeline with automatic EF Core instrumentation

Implementation Details

  • Added to Microsoft.AspNetCore.Builder namespace for WebApplicationBuilder extensions
  • Includes proper parameter validation with meaningful error messages
  • Uses AddDbContext for standard registration (vs AddDbContextPool used by the existing method)
  • Comprehensive unit tests covering all usage scenarios and error cases
  • Updated project documentation with usage examples

Comparison with Existing Method

Feature AddSqliteDbContext EnrichSqliteDatabaseDbContext
Builder Type IHostApplicationBuilder WebApplicationBuilder
API Surface Complex with settings class Simplified with parameters
Connection String Required parameter Defaults to "DefaultConnection"
OpenTelemetry Not included Built-in optional support
Target Use Case Advanced configuration Quick setup and onboarding

Both methods can coexist and serve different use cases - the existing method for advanced scenarios and the new method for simplified, common use cases.

Fixes #821.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • dnceng.pkgs.visualstudio.com
    • Triggering command: dotnet build /home/REDACTED/work/Aspire/Aspire/src/CommunityToolkit.Aspire.Microsoft.EntityFrameworkCore.Sqlite/ --verbosity normal (dns block)
    • Triggering command: dotnet msbuild src/CommunityToolkit.Aspire.Microsoft.EntityFrameworkCore.Sqlite/ -p:TargetFramework=net8.0 -t:restore (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI changed the title [WIP] Feature Request: Add builder.EnrichSqliteDatabaseDbContext<TDbContext>() for SQLite provider Feature: Add EnrichSqliteDatabaseDbContext extension method for WebApplicationBuilder Sep 5, 2025
Copilot AI requested a review from aaronpowell September 5, 2025 05:27
Copilot finished work on behalf of aaronpowell September 5, 2025 05:27
@github-actions github-actions bot added the Stale label Sep 11, 2025
@github-actions
Copy link
Contributor

Code Coverage

Package Line Rate Branch Rate Complexity Health
CommunityToolkit.Aspire.EventStore 100% 100% 46
CommunityToolkit.Aspire.GoFeatureFlag 100% 97% 74
CommunityToolkit.Aspire.Hosting.ActiveMQ 79% 40% 100
CommunityToolkit.Aspire.Hosting.ActiveMQ.MassTransit 1% 0% 14
CommunityToolkit.Aspire.Hosting.Adminer 73% 50% 20
CommunityToolkit.Aspire.Hosting.Azure.Dapr 63% 47% 982
CommunityToolkit.Aspire.Hosting.Azure.Dapr.Redis 97% 88% 36
CommunityToolkit.Aspire.Hosting.Azure.DataApiBuilder 100% 100% 22
CommunityToolkit.Aspire.Hosting.Bun 82% 71% 54
CommunityToolkit.Aspire.Hosting.Dapr 62% 50% 878
CommunityToolkit.Aspire.Hosting.DbGate 94% 50% 18
CommunityToolkit.Aspire.Hosting.Deno 84% 75% 72
CommunityToolkit.Aspire.Hosting.EventStore 94% 100% 18
CommunityToolkit.Aspire.Hosting.GoFeatureFlag 93% 50% 18
CommunityToolkit.Aspire.Hosting.Golang 87% 70% 28
CommunityToolkit.Aspire.Hosting.Java 69% 72% 120
CommunityToolkit.Aspire.Hosting.k6 58% 12% 20
CommunityToolkit.Aspire.Hosting.LavinMQ 78% 50% 18
CommunityToolkit.Aspire.Hosting.LavinMQ.MassTransit 1% 0% 14
CommunityToolkit.Aspire.Hosting.MailPit 91% 50% 14
CommunityToolkit.Aspire.Hosting.McpInspector 89% 55% 148
CommunityToolkit.Aspire.Hosting.Meilisearch 73% 57% 50
CommunityToolkit.Aspire.Hosting.Minio 93% 75% 48
CommunityToolkit.Aspire.Hosting.MongoDB.Extensions 96% 83% 36
CommunityToolkit.Aspire.Hosting.MySql.Extensions 100% 88% 78
CommunityToolkit.Aspire.Hosting.Ngrok 52% 35% 82
CommunityToolkit.Aspire.Hosting.NodeJS.Extensions 97% 78% 286
CommunityToolkit.Aspire.Hosting.Ollama 67% 70% 174
CommunityToolkit.Aspire.Hosting.OpenTelemetryCollector 66% 53% 89
CommunityToolkit.Aspire.Hosting.PapercutSmtp 92% 50% 10
CommunityToolkit.Aspire.Hosting.PostgreSQL.Extensions 99% 89% 84
CommunityToolkit.Aspire.Hosting.Python.Extensions 68% 55% 90
CommunityToolkit.Aspire.Hosting.RavenDB 63% 49% 136
CommunityToolkit.Aspire.Hosting.Redis.Extensions 100% 71% 48
CommunityToolkit.Aspire.Hosting.Rust 94% 83% 16
CommunityToolkit.Aspire.Hosting.Solr 96% 100% 14
CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects 76% 64% 154
CommunityToolkit.Aspire.Hosting.Sqlite 99% 89% 42
CommunityToolkit.Aspire.Hosting.SqlServer.Extensions 100% 87% 78
CommunityToolkit.Aspire.Hosting.SurrealDb 54% 37% 234
CommunityToolkit.Aspire.MassTransit.RabbitMQ 100% 100% 30
CommunityToolkit.Aspire.Meilisearch 97% 92% 68
CommunityToolkit.Aspire.Microsoft.Data.Sqlite 89% 85% 52
CommunityToolkit.Aspire.Microsoft.EntityFrameworkCore.Sqlite 61% 58% 114
CommunityToolkit.Aspire.Minio.Client 73% 59% 88
CommunityToolkit.Aspire.OllamaSharp 74% 69% 120
CommunityToolkit.Aspire.RavenDB.Client 60% 53% 237
CommunityToolkit.Aspire.SurrealDb 79% 63% 78
Summary 72% (8332 / 11569) 59% (2599 / 4418) 5250

Minimum allowed line rate is 60%

@aaronpowell aaronpowell marked this pull request as ready for review September 11, 2025 06:45
Copilot AI review requested due to automatic review settings September 11, 2025 06:45
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a new EnrichSqliteDatabaseDbContext<TDbContext> extension method that provides a simplified API for ASP.NET Core applications to register Entity Framework Core DbContext with SQLite provider, including optional OpenTelemetry instrumentation support.

Key changes:

  • New extension method targeting IHostApplicationBuilder with simplified parameter-based configuration
  • Added OpenTelemetry EF Core instrumentation support with optional disable flag
  • Comprehensive unit tests covering all usage scenarios and error validation

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
AspireEFSqliteExtensions.cs Added new EnrichSqliteDatabaseDbContext method and refactored instrumentation configuration
SqliteEntityFrameworkCoreSettings.cs Added DisableTracing property to control OpenTelemetry instrumentation
EnrichSqliteDatabaseDbContextTests.cs New test file with comprehensive coverage of the new extension method
CommunityToolkit.Aspire.Microsoft.EntityFrameworkCore.Sqlite.csproj Added OpenTelemetry package references
README.md Updated documentation with new usage examples and comparison table
Directory.Packages.props Added OpenTelemetry.Instrumentation.EntityFrameworkCore package
Directory.Build.props Updated OpenTelemetry version to 1.12.0
Comments suppressed due to low confidence (1)

src/CommunityToolkit.Aspire.Microsoft.EntityFrameworkCore.Sqlite/SqliteEntityFrameworkCoreSettings.cs:1

  • The comment incorrectly refers to 'PostgreSQL database' when this is a SQLite settings class. It should say 'SQLite database'.
namespace Microsoft.Extensions.Hosting;

]);

// Act
builder.EnrichSqliteDatabaseDbContext<TestDbContext>(settings => settings.DisableTracing = true);
Copy link

Copilot AI Sep 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test method name suggests it tests the enableOpenTelemetry parameter, but the implementation uses a settings configuration delegate instead. The method signature in the actual implementation doesn't match what's being tested here.

Copilot uses AI. Check for mistakes.
]);

// Act
builder.EnrichSqliteDatabaseDbContext<TestDbContext>(settings => settings.DisableTracing = false);
Copy link

Copilot AI Sep 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test method name suggests it tests the default OpenTelemetry behavior, but it's explicitly setting DisableTracing = false. This test should either call the method without parameters to test the actual default behavior, or rename the test to reflect what it's actually testing.

Suggested change
builder.EnrichSqliteDatabaseDbContext<TestDbContext>(settings => settings.DisableTracing = false);
builder.EnrichSqliteDatabaseDbContext<TestDbContext>();

Copilot uses AI. Check for mistakes.
);

configureSettings?.Invoke(settings);

Copy link

Copilot AI Sep 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The connection string is not validated before use. Unlike the existing AddSqliteDbContext method which validates the connection string, this new method directly passes settings.ConnectionString to UseSqlite without checking if it's null or empty.

Suggested change
if (string.IsNullOrWhiteSpace(settings.ConnectionString))
{
throw new ArgumentException("The connection string for Sqlite DbContext cannot be null or empty.", nameof(settings.ConnectionString));
}

Copilot uses AI. Check for mistakes.
@github-actions github-actions bot removed the Stale label Sep 12, 2025
@aaronpowell aaronpowell merged commit f9496b0 into main Sep 15, 2025
104 checks passed
@aaronpowell aaronpowell deleted the copilot/fix-821 branch September 15, 2025 04:02
@aaronpowell aaronpowell added this to the 9.8 milestone Sep 24, 2025
FullStackChef pushed a commit to FullStackChef/CommunityToolkit-Aspire that referenced this pull request Sep 26, 2025
…plicationBuilder (CommunityToolkit#837)

* Initial plan

* Add EnrichSqliteDatabaseDbContext extension method for WebApplicationBuilder

Co-authored-by: aaronpowell <[email protected]>

* Complete EnrichSqliteDatabaseDbContext with OpenTelemetry support

Co-authored-by: aaronpowell <[email protected]>

* Add documentation and complete test coverage for EnrichSqliteDatabaseDbContext

Co-authored-by: aaronpowell <[email protected]>

* Aligning properly with the aspire implementations

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: aaronpowell <[email protected]>
Co-authored-by: Aaron Powell <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: Add builder.EnrichSqliteDatabaseDbContext<TDbContext>() for SQLite provider

2 participants