Skip to main content
net8.0

A Microsoft SQL Server provider for Entity Framework Core that integrates with Aspire, including connection pooling, health check, logging, and telemetry.

Published
Apr 9, 2024
2y ago
Total Downloads
6.5M
6,486,741

Deprecated Package Version

Reason: Other, Legacy

This version is out of support and is no longer maintained. Please upgrade to the latest version. See our support policy for details: https://aka.ms/dotent/aspire/support

Install

dotnet add package Aspire.Microsoft.EntityFrameworkCore.SqlServer --version 8.0.0-preview.5.24201.12
ProductVersions
.NET
net8.0

README

Aspire.Microsoft.EntityFrameworkCore.SqlServer library

Registers EntityFrameworkCore DbContext service for connecting Azure SQL, MS SQL server database. Enables connection pooling, retries, health check, logging and telemetry.

Getting started

Prerequisites

  • Azure SQL or MS SQL server database and connection string for accessing the database.

Install the package

Install the .NET Aspire SQL Server EntityFrameworkCore SqlClient library with NuGet:

dotnet add package Aspire.Microsoft.EntityFrameworkCore.SqlServer

Usage example

In the Program.cs file of your project, call the AddSqlServerDbContext extension method to register a DbContext for use via the dependency injection container. The method takes a connection name parameter.

builder.AddSqlServerDbContext<MyDbContext>("sqldata");

You can then retrieve the MyDbContext instance using dependency injection. For example, to retrieve the context from a Web API controller:

private readonly MyDbContext _context;

public ProductsController(MyDbContext context)
{
    _context = context;
}

You might also need to configure specific option of Sql Server, or register a DbContext in other ways. In this case call the EnrichSqlServerDbContext extension method, for example:

var connectionString = builder.Configuration.GetConnectionString("catalogdb");
builder.Services.AddDbContextPool<CatalogDbContext>(dbContextOptionsBuilder => dbContextOptionsBuilder.UseSqlServer(connectionString));
builder.EnrichSqlServerDbContext<CatalogDbContext>();

Configuration

The .NET Aspire SQL Server EntityFrameworkCore SqlClient component provides multiple options to configure the SQL connection based on the requirements and conventions of your project.

Use a connection string

When using a connection string from the ConnectionStrings configuration section, you can provide the name of the connection string when calling builder.AddSqlServerDbContext<TContext>():

builder.AddSqlServerDbContext<MyDbContext>("myConnection");

And then the connection string will be retrieved from the ConnectionStrings configuration section:

{
  "ConnectionStrings": {
    "myConnection": "Data Source=myserver;Initial Catalog=master"
  }
}

The EnrichSqlServerDbContext won't make use of the ConnectionStrings configuration section since it expects a DbContext to be registered at the point it is called.

See the ConnectionString documentation for more information on how to format this connection string.

Use configuration providers

The .NET Aspire SQL Server EntityFrameworkCore SqlClient component supports Microsoft.Extensions.Configuration. It loads the MicrosoftEntityFrameworkCoreSqlServerSettings from configuration by using the Aspire:Microsoft:EntityFrameworkCore:SqlServer key. Example appsettings.json that configures some of the options:

{
  "Aspire": {
    "Microsoft": {
      "EntityFrameworkCore": {
        "SqlServer": {
          "HealthChecks": false,
          "Tracing": false
        }
      }
    }
  }
}

Use inline delegates

Also you can pass the Action<MicrosoftEntityFrameworkCoreSqlServerSettings> configureSettings delegate to set up some or all the options inline, for example to disable health checks from code:

    builder.AddSqlServerDbContext<MyDbContext>("sqldata", settings => settings.HealthChecks = false);

or

    builder.EnrichSqlServerDbContext<MyDbContext>(settings => settings.HealthChecks = false);

AppHost extensions

In your AppHost project, install the Aspire.Hosting.SqlServer library with NuGet:

dotnet add package Aspire.Hosting.SqlServer

Then, in the Program.cs file of AppHost, register a SqlServer database and consume the connection using the following methods:

var sql = builder.AddSqlServer("sql").AddDatabase("sqldata");

var myService = builder.AddProject<Projects.MyService>()
                       .WithReference(sql);

The WithReference method configures a connection in the MyService project named sqldata. In the Program.cs file of MyService, the sql connection can be consumed using:

builder.AddSqlServerDbContext<MyDbContext>("sqldata");

Additional documentation

Feedback & contributing

https://github.com/dotnet/aspire

Changes from Previous Version

v8.0.0-preview.4.24156.9v8.0.0-preview.5.24201.12

Adoption Assessment

A verified publisher signal is available. Assess fit from the package's supported frameworks, license and dependency graph rather than popularity alone.

This version is marked prerelease. Validate its API and upgrade path before production use.NuGet marks this package as deprecated. Review the publisher's migration guidance before adopting it.Compatibility metadata lists 1 target framework; confirm the exact target used by your application.

Related Guides & Workflows

Learn how to evaluate, integrate, or audit this package in your .NET project.