Reputation: 2987
I am having some trouble while inserting object into MongoDb in C#. My code is just like this:
_db.GetCollection<LogRecord> (collectionName).InsertOneAsync (log);
The object that I'm trying to insert is just like this:
When I check in my collection, Status object is always missing:
My object:
public class LogRecord
{
public Dictionary<string, QueryDetailsRecord> QueryDetails { get; set; }
}
public class QueryDetailsRecord
{
public double ElapsedTime { get; set; }
public bool InfoFound { get; set; }
// Cache vars
public int CacheValidDays { get; set; }
public bool CacheEnabled { get; set; }
public bool CacheUsed { get; set; }
// Errors codes
public StatusRecord Status { get; set; }
public string ExceptionMessage { get; set; }
/// <summary>
/// Constructor
/// </summary>
public QueryDetailsRecord() {}
/// <summary>
/// Constructor
/// </summary>
/// <param name="elapsedTime">Elapsed time</param>
/// <param name="status">Status object</param>
/// <param name="exceptionMessage">Error message</param>
public QueryDetailsRecord (double elapsedTime, StatusRecord status, string exceptionMessage)
{
ElapsedTime = elapsedTime;
Status = status;
ExceptionMessage = exceptionMessage;
}
}
public class StatusRecord
{
private int _code = -1;
private string _msg = null;
public int Code
{
get { return _code; }
}
public string Message
{
get { return _msg; }
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="code">Status code</param>
/// <param name="msg">Status message</param>
public StatusRecord (int code, string msg)
{
_code = code;
_msg = msg;
}
}
Does anyone have a clue of what's happing here?
Thank you!
Upvotes: 1
Views: 660
Reputation: 493
I had this same issue a few days ago. Maybe it's happening because the properties "code" and "message" defined on your "Status" Dictionary are private, this way the method "InsertOneAsync" cannot "see" them.
Try to define "code" and "message" as public and see if it works.
Upvotes: 3