mbdavid / LiteDB

LiteDB - A .NET NoSQL Document Store in a single data file
http://www.litedb.org
MIT License
8.62k stars 1.25k forks source link

[BUG] BsonCtor with parameters #2390

Closed bakirov-eldar closed 6 months ago

bakirov-eldar commented 1 year ago

Version 5.0.17 Windows 10 22H2 .NET 8.0

Describe the bug If id is ulong, then an error is thrown. LiteDB.LiteException: "Failed to create instance for type ..... Checks if the class has a public constructor with no parameters.

Code to Reproduce

public class SimpleMessage
{
    [BsonId(false)]
    public ulong Id { get; set; }
    public string Content { get; set; }
    [BsonCtor]
    public SimpleMessage(ulong id, string content)
    {
        Id = id; 
        Content = content;
    }
}
public void SaveAndLoadModel()
{
    using (var db = new LiteDatabase("model.ldb", new BsonMapper().UseCamelCase()))
    {
        var simpleMsgs = db.GetCollection<SimpleMessage>();
        try
        {
            var msg = new SimpleMessage(1174798982187391628, "Test message");
            Console.WriteLine($"Database Msg: {msg.Id}, content: {msg.Content}");
            simpleMsgs.Insert(msg);
            var elements = simpleMsgs.FindAll();
            foreach (var el in elements)
            {
                Console.WriteLine($"Database MsgId: {el.Id}, content: {el.Content}");
            }
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex);
        }
    }
}