Class MongoDbConnection
- Namespace
- Microsoft.Extensions.DependencyInjection
- Assembly
- Arc4u.MongoDB.dll
Registration of a MongoDB database context in the service collection.
public static class MongoDbConnection
- Inheritance
-
MongoDbConnection
- Inherited Members
Methods
AddMongoDatabase<TContext>(IServiceCollection, IConfiguration, string)
Registers the database context and its IMongoClientFactory<TContext>, with the client settings read from a connection string.
public static void AddMongoDatabase<TContext>(this IServiceCollection services, IConfiguration configuration, string connectionStringKey) where TContext : DbContext, new()
Parameters
servicesIServiceCollectionThe service collection.
configurationIConfigurationThe configuration holding the connection string.
connectionStringKeystringThe name of the connection string in the
ConnectionStringssection.
Type Parameters
TContextThe type of the database context.
Examples
services.AddMongoDatabase<ShopContext>(configuration, "ShopDb");
Remarks
The database name is the one of the connection string. There is one context per database, and its collections are mapped in DbContext. The settings are registered as named options, the name being the database name in lower case. The context is created once and registered as a singleton.
AddMongoDatabase<TContext>(IServiceCollection, IConfiguration, string, Action<MongoClientSettings>)
Registers the database context and its IMongoClientFactory<TContext>, with the client settings read from a connection string and completed by a delegate.
public static void AddMongoDatabase<TContext>(this IServiceCollection services, IConfiguration configuration, string connectionStringKey, Action<MongoClientSettings> options) where TContext : DbContext, new()
Parameters
servicesIServiceCollectionThe service collection.
configurationIConfigurationThe configuration holding the connection string.
connectionStringKeystringThe name of the connection string in the
ConnectionStringssection.optionsAction<MongoClientSettings>Configures the MongoDB.Driver.MongoClientSettings created from the connection string.
Type Parameters
TContextThe type of the database context.
Examples
services.AddMongoDatabase<ShopContext>(configuration, "ShopDb", settings =>
settings.TranslationOptions = new ExpressionTranslationOptions { CompatibilityLevel = ServerVersion.Server60 });
Remarks
The delegate runs on the settings created from the connection string, so it can set what a connection string cannot carry, such as
MongoDB.Driver.MongoClientSettings.TranslationOptions (the LINQ translation used by AsQueryable() and OData), MongoDB.Driver.MongoClientSettings.ServerApi
or MongoDB.Driver.MongoClientSettings.ClusterConfigurator. Otherwise it behaves as AddMongoDatabase<TContext>(IServiceCollection, IConfiguration, string).
Exceptions
- ArgumentNullException
optionsis null.
AddMongoDatabase<TContext>(IServiceCollection, string, Action<MongoClientSettings>)
Registers the database context and its IMongoClientFactory<TContext>, with the client settings given by a delegate.
public static void AddMongoDatabase<TContext>(this IServiceCollection services, string databaseName, Action<MongoClientSettings> options) where TContext : DbContext, new()
Parameters
servicesIServiceCollectionThe service collection.
databaseNamestringThe name of the database.
optionsAction<MongoClientSettings>Configures the MongoDB.Driver.MongoClientSettings.
Type Parameters
TContextThe type of the database context.
Examples
services.AddMongoDatabase<ShopContext>("shop", settings => settings.Server = new MongoServerAddress("localhost", 27017));
Remarks
The settings are registered as named options, the name being the database name in lower case. The context is created once and registered as a singleton.