gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 09:39:31 27 4
gpt4 key购买 nike

我正在尝试熟悉从 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();
}
}

最佳答案

GetServer() 已被弃用,像这样从客户端检索数据库:

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

关于c# - 'MongoDB.Driver.MongoClient' 不包含 'GetServer' 的定义 ...可以找到类型为 'MongoDB.Driver.MongoClient' 的第一个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40441617/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com