Model:
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; }
}
Question Collection In 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"
}
]
}
Get Question Method:
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);
}
}
When I try to retrieve question, I get following exception:
An exception of type 'System.IO.FileFormatException' occurred in MyProj.dll but was not handled in user code
Additional information: An error occurred while deserializing the PreRenderedQuestionsList property of class Data.QuestionModel: Element 'Id' does not match any field or property of class
I am able to add and update Question Collection, but not able to retrieve data. What I am missing?
[BsonElement("Id")]to it.BsonType.ObjectId. You should probably useObjectIdinstead...