Share via


AspireSqlServerEFCoreSqlClientExtensions.AddSqlServerDbContext<TContext> Method

Definition

Registers the given DbContext as a service in the services provided by the builder. Enables db context pooling, retries, health check, logging and telemetry for the DbContext.

public static void AddSqlServerDbContext<TContext> (this Microsoft.Extensions.Hosting.IHostApplicationBuilder builder, string connectionName, Action<Aspire.Microsoft.EntityFrameworkCore.SqlServer.MicrosoftEntityFrameworkCoreSqlServerSettings>? configureSettings = default, Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder>? configureDbContextOptions = default) where TContext : Microsoft.EntityFrameworkCore.DbContext;
static member AddSqlServerDbContext : Microsoft.Extensions.Hosting.IHostApplicationBuilder * string * Action<Aspire.Microsoft.EntityFrameworkCore.SqlServer.MicrosoftEntityFrameworkCoreSqlServerSettings> * Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> -> unit (requires 'Context :> Microsoft.EntityFrameworkCore.DbContext)
<Extension()>
Public Sub AddSqlServerDbContext(Of TContext As DbContext) (builder As IHostApplicationBuilder, connectionName As String, Optional configureSettings As Action(Of MicrosoftEntityFrameworkCoreSqlServerSettings) = Nothing, Optional configureDbContextOptions As Action(Of DbContextOptionsBuilder) = Nothing)

Type Parameters

TContext

The DbContext that needs to be registered.

Parameters

builder
IHostApplicationBuilder

The IHostApplicationBuilder to read config from and add services to.

connectionName
String

A name used to retrieve the connection string from the ConnectionStrings configuration section.

configureSettings
Action<MicrosoftEntityFrameworkCoreSqlServerSettings>

An optional delegate that can be used for customizing options. It's invoked after the settings are read from the configuration.

configureDbContextOptions
Action<DbContextOptionsBuilder>

An optional delegate to configure the DbContextOptions for the context.

Exceptions

Thrown if mandatory builder is null.

Thrown when mandatory ConnectionString is not provided.

Remarks

Reads the configuration from "Aspire:Microsoft:EntityFrameworkCore:SqlServer:{typeof(TContext).Name}" config section, or "Aspire:Microsoft:EntityFrameworkCore:SqlServer" if former does not exist.

Applies to