Class DefaultMongoClientFactory<TContext>
Default IMongoClientFactory<TContext>: creates the client lazily, once, from the named MongoDB.Driver.MongoClientSettings of the database.
public class DefaultMongoClientFactory<TContext> : IMongoClientFactory<TContext> where TContext : DbContext
Type Parameters
TContextThe type of the database context.
- Inheritance
-
DefaultMongoClientFactory<TContext>
- Implements
-
IMongoClientFactory<TContext>
- Inherited Members
Constructors
DefaultMongoClientFactory(IOptionsMonitor<MongoClientSettings>, IServiceProvider)
Creates the factory.
public DefaultMongoClientFactory(IOptionsMonitor<MongoClientSettings> clientSettings, IServiceProvider serviceProvider)
Parameters
clientSettingsIOptionsMonitor<MongoClientSettings>The named client settings; the name is the database name in lower case.
serviceProviderIServiceProviderUsed to resolve the registered
TContext.
Exceptions
- InvalidOperationException
TContextis not registered.
Methods
CreateClient()
Gets the MongoDB client of the context.
public IMongoClient CreateClient()
Returns
- IMongoClient
The client.
Remarks
The client is created once from the named MongoDB.Driver.MongoClientSettings of the database.
Exceptions
- MongoClientException
No MongoDB.Driver.MongoClientSettings are registered under the database name in lower case.
GetCollection<TEntity>()
Gets the collection the entity type is mapped to in the DbContext.
public IMongoCollection<TEntity> GetCollection<TEntity>()
Returns
- IMongoCollection<TEntity>
The collection.
Type Parameters
TEntityThe document type of the collection.
Exceptions
- TypeNotMappedToCollectionException
TEntityis not mapped to a collection.- TypeMappedToMoreThanOneCollectionException<TEntity>
TEntityis mapped to several collections.
GetCollection<TEntity>(string)
Gets a collection of the database by name.
public IMongoCollection<TEntity> GetCollection<TEntity>(string collectionName)
Parameters
collectionNamestringThe name of the collection.
Returns
- IMongoCollection<TEntity>
The collection.
Type Parameters
TEntityThe document type of the collection.