public static IServiceCollection AddMongoDb(this IServiceCollection services, IConfiguration configuration) { if (BsonClassMap.IsClassMapRegistered(typeof(Domain.Screenshot))) { BsonClassMap.RegisterClassMap <Domain.Screenshot>(cm => { cm.MapIdField(s => s.Id); cm.AutoMap(); }); } services.AddSingleton <IMongoContext>(_ => { var mongoSettings = new MongoDbSettings(); configuration.GetSection("MongoDbSettings").Bind(mongoSettings); return(new MongoContext(mongoSettings)); }); return(services); }
public MongoContext(MongoDbSettings dbSettings) { var mongoClient = new MongoClient(dbSettings.ConnectionString); _database = mongoClient.GetDatabase(dbSettings.DatabaseName); }