Search code examples
c#litedb

Why I am getting Serialization and deserialization of 'System.Type' instances are not supported. Path: $.EntityMapper.ForType.?


I am new to LiteDatabase. I have the following db.

using (var db = new LiteDatabase(@"MyData.db"))
{
    var users = db.GetCollection<User>("users");
    return new JsonResult(users);
}

When I try convert users to json.

var d = JsonConvert.SerializeObject(users);

I am getting

Serialization and deserialization of 'System.Type' instances are not supported. Path: $.EntityMapper.ForType.


Solution

  • Use FindAll()

    return new JsonResult(users.FindAll());
    

    GetCollection<T> returns ILiteCollection<T> which is implemented by LiteCollection<T> which has members storing type info (like EntityMapper with Type ForType property).