Creating the MongoDbRepository class

To create a MongoDbRepository class, we need to perform the following steps:

  1. Create a new folder called Data in your solution. Inside that folder, create a new class called MongoDBRepository:
  1. 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); } } ...

Get C# 7 and .NET Core 2.0 Blueprints now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.