我正在重构我的webapp以使用最后一个mongo驱动程序c#功能作为异步方法。
查找异步方法现在运行良好,但我遇到了UpdateOneAsync问题。
这是我在DAL中的方法:
public static async Task<bool> UpdateOne<T>(FilterDefinition<T> filterDefinition, UpdateDefinition<T> updateDefinition)
{
var coll = GetCollection<T>();
var result = await coll.UpdateOneAsync(filterDefinition, updateDefinition);
return result.ModifiedCount > 0;
}
它是从另一个类调用的:
public virtual async Task<bool> UpdateField<T>(Expression<Func<T, bool>> findExpression, Expression<Func<T, dynamic>> fieldExpression, dynamic value)
{
try
{
var filter = Builders<T>.Filter.Where(findExpression);
UpdateDefinition<T> updateFilter = Builders<T>.Update.Set(fieldExpression, value);
var updateResult = await MongoClientWrapper.UpdateOne(filter, updateFilter);
return updateResult;
}
catch (Exception ex)
{
_logMongoRepo.ErrorFormat("(MongoRepo) Error on UpdateField : {0}", ex);
return false;
}
}
UpdateOneAsync返回的错误是:
Unable to cast object of type 'MongoDB.Bson.Serialization.Serializers.StringSerializer' to type 'MongoDB.Bson.Serialization.IBsonSerializer`1[System.Object]
当我尝试将UpdateOneAsync与我的对象Type一起使用时,没关系。
我真的不知道错误的来源:/