Crud with Mongodb and C# error in type or namespace

Asked

Viewed 63 times

1

Accompanying a Macoratti tutorial here, errors appear in three types. I don’t know what to add to solve. I Copy and Paste and gave 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;
            }
        }
    }
}

Mistake here: Mongodatabase => Type or Namespace cannot be found

Mistake here: Getserver => Mongoclient does not contain a definition for Getserver

Mistake here: Mongocollection => Type or Namespace cannot be found

How do I fix it?

1 answer

1


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

About the reported problems I made the following modifications:

Mongodatabase : Change to IMongoDatabase

Getserver : Remove this function and directly get the DB instance

Mongocollection : Switch 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;
        }
    }
}
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.