使用Mongodb时查询结果问题报错问题

一本正经道 2016-08-11 04:49:33
请教各位,我用官方的1.11的驱动,查询时collection.FindAllAs<T>()方法返回MongoCursor这个对象结果该怎样还原为实体对象呢?

我的实体对象中的CurrentObj包含了另一个实体对象,bson结构如下:

/* 1 */
{
"_id" : ObjectId("57abebbe1f28d84e43ede3f9"),
"_t" : "AModel",
"AAId" : 11111,
"CurrentObj" : {
"_t" : "BModel",
"BBId" : 88888
},
"MinTime" : Date(-62135596800000),
"MaxTime" : Date(-62135596800000)
}



我的代码:

public static List<T> GetAll<T>(AModel model)// 传进来的T也是AModel
{
if (model == null || Utils.StrIsNullOrEmpty(model.CollectionName))
{
return null;
}
InitMongoDB(model.CollectionName);
MongoCursor<T> result = mDBProvider.GetAll<T>();// 这里已查询出结果
List<T> resultList = mDBProvider.CursorToList<T>(result);// 调用下面的遍历方法
return resultList;
}

public List<T> CursorToList<T>(MongoCursor<T> cursor)
{
List<T> resultList = new List<T>();
cursor.GetEnumerator().MoveNext();// 这里报错
if(cursor.Count() > 0)
{
foreach (var obj in cursor)// 遍历
{
resultList.Add(obj);
}
}
return resultList;
}


运行到MoveNext()时的报错详情:
An error occurred while deserializing the CurrentObj property of class AModel: Unknown discriminator value 'BModel'.

应该时CurrentObj里面的BModel对象没有正确反序列化,请教该怎样处理?

AModel实体的定义:

public class AModel
{
public int AAId {get; set; }
public Object CurrentObj { get; set; }
}


先感谢诸位~
...全文
1021 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
一本正经道 2016-08-13
  • 打赏
  • 举报
回复
一本正经道 2016-08-12
  • 打赏
  • 举报
回复
引用 3 楼 shingoscar 的回复:
BModel呐?
就一个BBId属性,所以没贴了…… public class BModel { public int BBId{get; set; } }
Poopaye 2016-08-11
  • 打赏
  • 举报
回复
BModel呐?
一本正经道 2016-08-11
  • 打赏
  • 举报
回复
咋没人捏
一本正经道 2016-08-11
  • 打赏
  • 举报
回复
知道的朋友请留步

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧