Skip to content

[BUG] BsonRef collection is not saved or retrieved #2736

@zorgoz

Description

@zorgoz

Version
Windows 10.
Same with library version 5.0.21 and 6.0.0.-pre74

Describe the bug
When using a collection annotated with DbRef (regardless of attribute or fluent), the master entity is saved with references, but when retrieved, the referenced entities are not loaded. No visible collection is created for the referenced entity.

More attempts here: https://stackoverflow.com/questions/79873876/litedb-does-not-load-bsonref-items?noredirect=1#comment140953309_79873876

Code to Reproduce

void Main()
{
    var fn = Path.Join(Path.GetTempPath(), "tmp2.db").Dump("DB");

    File.Delete(fn);
    var db = new LiteDatabase($"Filename={fn}; Connection=shared");

    var col = db.GetCollection<Master>();   

    col.Insert(new Master() { MasterData = "1", Details = [new() { Id = 1, Data =  "A" }, new() { Id = 2, Data =  "B" }, new() { Id = 3, Data =  "Y" } ] });
    col.Insert(new Master() { MasterData = "2", Details = [new() { Id = 4, Data =  "M" }, new() { Id = 5, Data =  "N" } ] });
    col.Insert(new Master() { MasterData = "3", Details = [new() { Id = 6, Data =  "X" } ] });

    col
        .Include(x => x.Details)
        .FindOne(_ => true)
        .Dump();

    col
        .Query()
        .Include(x => x.Details)
        .First()
        .Dump();
    
    db.Dispose();
    
    var repo = new LiteRepository($"Filename={fn}; Connection=shared");
    
    repo.Query<Master>()
        .Include(x => x.Details)
        .First()
        .Dump();
}

public class Master 
{
    [BsonId]
    public ObjectId Id { get; set; }
    public required string MasterData { get; set; } 
    [BsonRef]
    public List<Detail> Details { get; set; }
}

public class Detail
{
    [BsonId]
    public int Id { get; set; }

    public required string Data { get; set; }
}

Expected behavior
Details collection to be created, populated with the proper content and retrieved when included.

Screenshots/Stacktrace

Image Image

Additional context
By removing DbRef, collection is embedded.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions