To create a MongoDbRepository class, we need to perform the following steps:
- Create a new folder called Data in your solution. Inside that folder, create a new class called MongoDBRepository:
- Inside this class, add the following code:
public class MongoDBRepository { public readonly IMongoDatabase Database; public MongoDBRepository(IOptions<Settings> settings) { try { var mclient = new MongoClient(settings.Value.ConnectionString); Database = mclient.GetDatabase(settings.Value.Database); } catch (Exception ex) { throw new Exception("There was a problem connecting to the MongoDB database", ex); } } ...