litedb-org / LiteDB

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

[BUG] The LiteDB not shower value type long to private property #2530

Closed salesHgabriel closed 3 months ago

salesHgabriel commented 3 months ago

Version LiteDb 5.0.21 on .net 8

Describe the bug I'm trying to use a long property with a private set, but when trying to perform a query, it doesn't display the value, but if I leave it public and it can load the value.

If private property the response is zero, but removes private, value is showing according to database.

Code to Reproduce

using LiteDB;

Console.WriteLine("--------- PLAYGROUND ----------");

string dirApp = Directory.GetParent(Directory.GetParent(Directory.GetParent(Environment.CurrentDirectory).ToString()).ToString()).ToString() + "\\Examples\\"; 

var path = dirApp + "MyData.db";

using var db = new LiteDatabase(path);

var colUser = db.GetCollection<User>("user");

//uncomment to set values on database
//var user = new User(Guid.NewGuid()) { Name = "dadsd" };

//user.SetTicks();

//colUser.Insert(user);

var listUser = colUser.Query().ToArray().Select(a => a.Tick);

Console.WriteLine(string.Join(", ", listUser));

public class User : BaseEntity
{

    public User(Guid id) : base(id)
    {
    }

    public string Name { get; set; } = string.Empty;
}

public abstract class BaseEntity
{

    protected BaseEntity(Guid id) : base() => Id = id;

    public void SetTicks() => Tick = DateTime.UtcNow.Ticks;

    [BsonId]
    public Guid Id { get; private set; }

    public long Tick { get; private set; }
}
ssteiner commented 3 months ago

it is stated in the documentation that properties need to be public. I'm guessing the Deserialization from the BsonObject (used internally to store everything) to your User class works only on properties with a public setter - this isn't something that's specific to LiteDb, you'll have the same issue with Newtonsoft.Json for instance.

I think you'll need to create your own BsonMapper. The Serializer needs to know how to set this property that doesn't have a public setter (I'm guessing there's a method that leads to the value being set)

salesHgabriel commented 3 months ago

Tks for your answer, I change my properties to public