无法将类型为"MongoDB.Bson.Serialization.Serializers.DateTimeSerializer"的对象转换为类型为"MongoDB.Bson.Serialization.IBsonSerializer"的对象. [英] Unable to cast object of type 'MongoDB.Bson.Serialization.Serializers.DateTimeSerializer' to type 'MongoDB.Bson.Serialization.IBsonSerializer`
问题描述
在寻找解决方案时,我得到了此和
While searching for the solution I got this and this but the concept was not clear to me so couldn't implement it :(. This error occurs when I try to update values in the database(specifically for datetime objects). Below is the code that I'm using:-
var update = Builders<Model>.Update
.Set(t => t.startdate, BsonValue(objModel.startdate));
var result = model.UpdateOne(query, update);
和BonValue函数如下:-
and BonValue function is as follows:-
private static BsonValue BsonValue(object obj)
{
if (obj == null)
return (BsonValue)obj ?? BsonNull.Value;
else if (obj.GetType() == typeof(string))
return new BsonString(obj.ToString());
else if (obj.GetType() == typeof(DateTime))
return new BsonDateTime(DateTime.SpecifyKind(DateTime.Parse(obj.ToString()), DateTimeKind.Utc));
else if (obj.GetType() == typeof(int))
return new BsonInt32(int.Parse(obj.ToString()));
else
return (BsonValue)obj;
}
我认为这是因为我已经开始使用mongoDB.Bson和mongoDB.Driver的升级包.当前版本是2.3.0
I think this is because I have started using upgraded packages of mongoDB.Bson and mongoDB.Driver. Current version is 2.3.0
任何帮助将不胜感激.
修改
1)在插入数据时我的工作如下:-
1) while Inserting data what i do is as follows:-
BsonDocument objBsonDoc = new BsonDocument {
{ "startdate",DataManager.GetBsonValue( objModel.startdate) }
}; return dbHelper.CreateDocument(Model.CollectionName, objBsonDoc);
在objBsonDoc中,我可以看到StartDate值,但是在CreateDocument命令StartDate之后没有将其保存在数据库中.
In objBsonDoc I can see the StartDate value but after CreateDocument command StartDate doesn't get saved in DB.
2)这是我在更新值时正在做的事情:-
2) This is what I'm doing while updating values:-
public static long Edit(Model objModel, string id)
{
IMongoCollection<Model> model = dbHelper.GetCollection<Model>(Model.CollectionName);
var query = Builders<Model>.Filter.Eq(t => t.id, ObjectId.Parse(id));
var update = Builders<Model>.Update
.Set(t => t.startdate, objModel.startdate);
var result = model.UpdateOne(query, update);
return result.MatchedCount;
}
在这里我也获得开始日期的值,并且还获得Modifyed = 1,但是当我检查数据库时它仍然显示开始状态为空.
here also i get the value for start date and also getting modifiedcount=1 but still when i check DB it shows null for start sate.
我的模型课:-
public class Model
{
public static string CollectionName
{
get { return "Collection"; }
}
public ObjectId id { get; set; }
[BsonDateTimeOptions(Kind = DateTimeKind.Utc)]
[BsonRepresentation(BsonType.DateTime)]
public DateTime startdate { get; set; }
}
推荐答案
您不需要按照自己的方式执行自己的转换.
You shouldn't need to perform your own conversion the way you are.
尝试在模型上设置BsonRepresentation
Try setting the BsonRepresentation on your model
public class Model
{
[BsonDateTimeOptions(Kind = DateTimeKind.Utc)]
[BsonRepresentation(BsonType.DateTime)]
public DateTime startDate { get; set; }
}
为了澄清起见,我可以建议您将集合变量重命名为collection,而不是像model这样混淆,所以
For the sake of clarify can i suggest you rename your collection variable to collection instead of model as this in confusing, so
IMongoCollection<Model> collection = dbHelper.GetCollection<Model>(Model.CollectionName);
Model objModel = new Model { startDate = DateTime.UtcNow };
collection.InsertOne(yourModel);
要执行更新,您无需设置为BsonValue
And to perform your update you won't need to set to BsonValue
var query = Builders<Model>.Filter.Eq(t => t.Id, ObjectId.Parse(id));
var update = Builders<Model>.Update.Set(t => t.startdate, objModel.startdate);
var result = model.UpdateOne(query, update);
一个建议是将ObjectId存储为字符串,并像使用DateTime一样使用BsonRepresentation:
One suggestion, is to store your ObjectId as string and use BsonRepresentation as you have with DateTime:
[BsonRepresentation(BsonType.ObjectId)]
[BsonId]
public string Id { get; set; }
那样,您无需费劲将其解析为ObjectId,并且它的工作原理相同,因此您的过滤器将变为
That way you don't need to go out of your way to parse it to ObjectId and it works the same, so your filter becomes
var query = Builders<Model>.Filter.Eq(t => t.Id, id);
或者您可以像这样直接在更新中使用Lambda:
Or you could use Lambda directly in your update like so:
var update = Builders<Model>.Update
.Set(t => t.Id == id, objModel.startdate);
这篇关于无法将类型为"MongoDB.Bson.Serialization.Serializers.DateTimeSerializer"的对象转换为类型为"MongoDB.Bson.Serialization.IBsonSerializer"的对象.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!