型號:MongoDB中引發異常'System.IO.FileFormatException'反序列化屬性
public class QuestionModel
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
public string Name { get; set; }
public string Expression { get; set; }
[BsonIgnoreIfNull]
public List<PreRenderedQuestion> PreRenderedQuestionsList { get; set; }
}
public class PreRenderedQuestion
{
public string Id { get; set; }
public string Name { get; set; }
public string Expression { get; set; }
public string ExpressionWithValues { get; set; }
}
問題集在DB:
{
"_id" : ObjectId("5539b948bb63bc0680f29025"),
"Name" : "addition",
"Expression " : "a+b",
"PreRenderedQuestionsList" : [
{
"Id" : "5539b948bb63bc0680f29325",
"Name" : "addition",
"Expression " : "a+b",
"ExpressionWithValues " : "5+2"
},
{
"Id" : "5539b948bb63bc0680f29326",
"Name" : "addition",
"Expression " : "a+b",
"ExpressionWithValues " : "6+9"
}
]
}
獲取問題的方法:
function getQuestions(QuestionModel oModel)
{
_query = Query<QuestionModel>.Where(e => e.Is_Deleted == false);
_cursor = _collection.Find(_query);
oModel.QuestionList = new List<QuestionModel>();
foreach (QuestionModel ques in _cursor)
{
oModel.QuestionList.Add(ques);
}
}
當我嘗試找回的問題,我得到以下異常:
型「System.IO.FileFormatException」發生在 MyProj.dll但在用戶代碼
其他沒有處理的例外信息:發生錯誤,而反序列化類Data.QuestionModel的 PreRenderedQuestionsList屬性: 元素「ID」不匹配類的任何字段或屬性
我可以添加和更新問題集,但無法檢索數據。我錯過了什麼?
嘗試添加'[BsonElement(「Id」)]'。 – CodesInChaos
你的ID是一個字符串,而你說它是'BsonType.ObjectId'。你應該使用'ObjectId'來代替... – i3arnon
其實,這不應該發生,至少不是2.0驅動程序 - 你使用的驅動程序版本是什麼? – mnemosyn