Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting data from LiteDB

Tags:

c#

litedb

I wonder how I console.writeline data stored in my litedb database file. This is my POCO Class

[BsonId]
public int Id { get; set; }
public DateTime Updated { get; set; }
public DateTime Last { get; set; }
public override string ToString()
{
    return string.Format("Id : {0}, Updated : {1}, Last Message : {2}",
        Id,
        Updated,
        Last);
}

There i insert infomation in db:

using (var db = new LiteDatabase(_dbLocation))
{
    var hist = db.GetCollection<MailBoxInfo>("History");
    var mail = new MailBoxInfo
    {
        Updated = DateTime.Now,
        Last = datemsg
    };
    hist.Insert(mail);
    hist.EnsureIndex("Updated");
}

And, finally, try to output it:

using (var db = new LiteDatabase(_dbLocation))
{
    var hist = db.GetCollection<MailBoxInfo>("History");
    var res = hist.FindById(3);
}

Then I get exception

"Additional information: Failed to create instance for type 'TanganTask.MailBoxInfo'. Checks if has a public constructor with no parameters".

Where am I mistaken and how to solve this problem?

like image 229
Veotani Avatar asked Jul 05 '16 21:07

Veotani


1 Answers

LiteDB requires that your entity class must be public with an public constructor with no parameters. So, you class must be:

public class MailBoxInfo
{
    [BsonId]
    public int Id { get; set; }
    public DateTime Updated { get; set; }
    public DateTime Last { get; set; }
    public override string ToString()
    {
        return string.Format("Id : {0}, Updated : {1}, Last Message : {2}",
            Id,
            Updated,
            Last);
    }
}

Default classes are interal (just class MailBoxInfo).

like image 198
mbdavid Avatar answered Oct 23 '22 20:10

mbdavid