Crud with MongoDB and C # error in type or namespace

1

Following a tutorial from Macoratti here , errors appear in three types. I do not know what to add to solve it. I did a Copy and Paste and gave an error. Below the code:

using MongoDB.Driver;
using System;
using System.Configuration;

namespace Mvc_MongoDB.Models
{
    public class PaisDB
    {
        public MongoDatabase Database;
        public String DataBaseName = "PaisDB";
        string conexaoMongoDB = "";

        public PaisDB()
        {

            conexaoMongoDB = ConfigurationManager.ConnectionStrings["conexaoMongoDB"].ConnectionString;
            var cliente = new MongoClient(conexaoMongoDB);
            var server = cliente.GetServer();

            Database = server.GetDatabase(DataBaseName);
        }

        public MongoCollection<Pais> Paises
        {
            get
            {
                var Paises = Database.GetCollection<Pais>("Paises");
                return Paises;
            }
        }
    }
}

Error here: MongoDatabase = > Type or Namespace can not be found

Error here: GetServer = > MongoClient does not contain a definition for GetServer

Error here: MongoCollection = > Type or Namespace can not be found

How do I resolve this?

    
asked by anonymous 08.06.2018 / 20:44

1 answer

1

My dear, this may change depending on the version of the mongoDb driver but I suggest always downloading the most current version. The modifications I am going to suggest here were obtained from the MongoDb Official Documentation

On the problems reported I made the following modifications:

MongoDatabase: Change to IMongoDatabase

GetServer: Remove this function and directly get the instance of DB

MongoCollection: Change to IMongoCollection

using MongoDB.Driver;
using System;
using System.Configuration;

namespace Mvc_MongoDB.Models
{
public class PaisDB
{
    public IMongoDatabase Database;
    public String DataBaseName = "PaisDB";
    string conexaoMongoDB = "";

    public PaisDB()
    {

        conexaoMongoDB = ConfigurationManager.ConnectionStrings["conexaoMongoDB"].ConnectionString;
        var cliente = new MongoClient(conexaoMongoDB);
        Database = cliente.GetDatabase(DataBaseName);
    }

    public IMongoCollection<Pais> Paises
    {
        get
        {
            var Paises = Database.GetCollection<Pais>("Paises");
            return Paises;
        }
    }
}
}
    
08.06.2018 / 21:40