CosmosServiceCollectionExtensions.AddCosmos<TContext> Method

Definition

Registers the given Entity Framework DbContext as a service in the IServiceCollection and configures it to connect to an Azure Cosmos database.

public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddCosmos<TContext> (this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, string connectionString, string databaseName, Action<Microsoft.EntityFrameworkCore.Infrastructure.CosmosDbContextOptionsBuilder>? cosmosOptionsAction = default, Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder>? optionsAction = default) where TContext : Microsoft.EntityFrameworkCore.DbContext;
static member AddCosmos : Microsoft.Extensions.DependencyInjection.IServiceCollection * string * string * Action<Microsoft.EntityFrameworkCore.Infrastructure.CosmosDbContextOptionsBuilder> * Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> -> Microsoft.Extensions.DependencyInjection.IServiceCollection (requires 'Context :> Microsoft.EntityFrameworkCore.DbContext)
<Extension()>
Public Function AddCosmos(Of TContext As DbContext) (serviceCollection As IServiceCollection, connectionString As String, databaseName As String, Optional cosmosOptionsAction As Action(Of CosmosDbContextOptionsBuilder) = Nothing, Optional optionsAction As Action(Of DbContextOptionsBuilder) = Nothing) As IServiceCollection

Type Parameters

TContext

The type of context to be registered.

Parameters

serviceCollection
IServiceCollection

The IServiceCollection to add services to.

connectionString
String

The connection string of the database to connect to.

databaseName
String

The database name.

cosmosOptionsAction
Action<CosmosDbContextOptionsBuilder>

An optional action to allow additional Cosmos-specific configuration.

optionsAction
Action<DbContextOptionsBuilder>

An optional action to configure the DbContextOptions for the context.

Returns

The same service collection so that multiple calls can be chained.

Remarks

This method is a shortcut for configuring a DbContext to use Cosmos. It does not support all options. Use EntityFrameworkServiceCollectionExtensions.AddDbContext and related methods for full control of this process.

Use this method when using dependency injection in your application, such as with ASP.NET Core. For applications that don't use dependency injection, consider creating DbContext instances directly with its constructor. The OnConfiguring(DbContextOptionsBuilder) method can then be overridden to configure the Cosmos database provider.

To configure the DbContextOptions<TContext> for the context, either override the OnConfiguring(DbContextOptionsBuilder) method in your derived context, or supply an optional action to configure the DbContextOptions for the context.

See Using DbContextOptions, and Accessing Azure Cosmos DB with EF Core for more information and examples.

Applies to