Table of Contents

Method AddMongoDB

Namespace
Microsoft.Extensions.DependencyInjection
Assembly
MongoDB.EntityFrameworkCore.dll

AddMongoDB<TContext>(IServiceCollection, string, string, Action<MongoDbContextOptionsBuilder>?, Action<DbContextOptionsBuilder>?)

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

public static IServiceCollection AddMongoDB<TContext>(this IServiceCollection serviceCollection, string connectionString, string databaseName, Action<MongoDbContextOptionsBuilder>? mongoOptionsAction = null, Action<DbContextOptionsBuilder>? optionsAction = null) where TContext : DbContext

Parameters

serviceCollection IServiceCollection

The IServiceCollection to add services to.

connectionString string

The connection string of the MongoDB server to connect to.

databaseName string

The database name on the server.

mongoOptionsAction Action<MongoDbContextOptionsBuilder>

An optional action to allow additional MongoDB-specific configuration.

optionsAction Action<DbContextOptionsBuilder>

An optional action to configure the DbContextOptions for the context.

Returns

IServiceCollection

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

Type Parameters

TContext

The type of context to be registered.

AddMongoDB<TContext>(IServiceCollection, IMongoClient, string, Action<MongoDbContextOptionsBuilder>?, Action<DbContextOptionsBuilder>?)

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

public static IServiceCollection AddMongoDB<TContext>(this IServiceCollection serviceCollection, IMongoClient mongoClient, string databaseName, Action<MongoDbContextOptionsBuilder>? mongoOptionsAction = null, Action<DbContextOptionsBuilder>? optionsAction = null) where TContext : DbContext

Parameters

serviceCollection IServiceCollection

The IServiceCollection to add services to.

mongoClient IMongoClient

The MongoDB.Driver.IMongoClient to use to connect to the MongoDB server.

databaseName string

The database name on the server.

mongoOptionsAction Action<MongoDbContextOptionsBuilder>

An optional action to allow additional MongoDB-specific configuration.

optionsAction Action<DbContextOptionsBuilder>

An optional action to configure the DbContextOptions for the context.

Returns

IServiceCollection

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

Type Parameters

TContext

The type of context to be registered.