【发布时间】:2015-11-23 17:17:47
【问题描述】:
我正在添加一个UpdateCustomer 方法,该方法将修改后的客户传递到数据库中。但是我在更新的文档上调用ReplaceOneAsync 时遇到了错误。
我咨询了以下example 和api reference,它们都表示要传递ReplaceOneAsync 和filter 和document 参数。
但是具体的错误是因为参数不正确而抛出的,如下所述:
Error 1 The best overloaded method match for 'MongoDB.Driver.IMongoCollection<MongoDBApp.Models.CustomerModel>.ReplaceOneAsync(MongoDB.Driver.FilterDefinition<MongoDBApp.Models.CustomerModel>, MongoDBApp.Models.CustomerModel, MongoDB.Driver.UpdateOptions, System.Threading.CancellationToken)' has some invalid arguments
Error 2 Argument 2: cannot convert from 'MongoDB.Bson.BsonDocument' to 'MongoDBApp.Models.CustomerModel'
有人对理解错误有任何提示吗?
UpdateCustomer 方法:
public async Task UpdateCustomer(CustomerModel customer)
{
var collection = StartConnection();
var filter = Builders<CustomerModel>.Filter.Where(x => x.Id == customer.Id);
BsonDocument doc = new BsonDocument();
doc["_id"] = customer.Id;
doc["firstName"] = customer.FirstName;
doc["lastName"] = customer.LastName;
doc["email"] = customer.Email;
//error thrown here on the ReplaceOneAsync params..
await collection.ReplaceOneAsync(filter, doc);
}
以及相关的StartConnection方法:
private static IMongoCollection<CustomerModel> StartConnection()
{
var client = new MongoClient(connectionString);
var database = client.GetDatabase("orders");
//Get a handle on the customers collection:
var collection = database.GetCollection<CustomerModel>("customers");
return collection;
}
【问题讨论】:
标签: c# .net mongodb-.net-driver mongodb-csharp-2.0