'MongoDB.Driver.MongoClient'不包含'GetServer'的定义...可以找到'MongoDB.Driver.MongoClient'类型的第一个参数

时间:2016-11-05 18:14:13

标签: c# mongodb

我正在尝试熟悉从c#程序写入MongoDB。我根据http://mongodb.github.io/mongo-csharp-driver/1.11/getting_started/

的建议设置了我的代码

我正在尝试运行此程序但是收到此错误“'MongoDB.Driver.MongoClient'不包含'GetServer'的定义,也没有扩展方法'GetServer'接受类型为'MongoDB.Driver'的第一个参数。可以找到MongoClient'。我可以帮忙吗?

提前致谢, 田。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
//Additionally, you will frequently add one or more of these using statements:
//using MongoDB.Driver.Builders; //Error rebuilding when this statement is active: "Using the generic type 'MongoDB.Driver.Builders<TDocument>' requires 1 type arguments
//using MongoDB.Driver.GridFS;
using MongoDB.Driver.Linq;
//using MongoDB.Driver.MongoClient; //Error rebuilding when this statement is active "A using namespace directive can only be applied to namespaces; 'MongoDB.Driver.MongoClient' is a type not a namespace 

namespace write2MongoDb
{
    public class Entity
    {
        public ObjectId Id { get; set; }
        public string Name { get; set; }
    }


class Program
{
    static void Main(string[] args)
    {
        #region Full Sample Program
        var connectionString = "mongodb://localhost";
        var client = new MongoClient(connectionString);
        var server = client.GetServer();
        var database = server.GetDatabase("test");
        var collection = database.GetCollection<Entity>("entities");

        var entity = new Entity { Name = "Tom" };
        collection.Insert(entity);
        var id = entity.Id;

        var query = Query<Entity>.EQ(e => e.Id, id);
        entity = collection.FindOne(query);

        entity.Name = "Dick";
        collection.Save(entity);

        var update = Update<Entity>.Set(e => e.Name, "Harry");
        collection.Update(query, update);

        collection.Remove(query);


        #endregion


        Console.ReadKey();
    }
}

}

1 个答案:

答案 0 :(得分:2)

不推荐使用GetServer(),从客户端检索数据库,如下所示:

var client = new MongoClient("mongodb://localhost");
var database = client.GetDatabase("test");
var collection = database.GetCollection<Entity>("entities");