1

I'm struggling to insert/update my class with a list of list of object into my LiteDB database. I tried several things and possible solutions, but I can't get it working.

Here my classes:

Parent:

 [BsonRef("pictogram")]
public class Pictogram : BasePictogram, IPictogram
{
      /// <summary>
    /// each pictogram has an unique Id
    /// the Id tpye is Guid
    /// </summary>
    [BsonId]
    public Guid Id { get; }

    /// <summary>
    /// The 2D Array (list of list) which contain the whole matrix of Pixels in it
    /// </summary>°
    public List<List<IPixel>> Pixels { get; set; }
}

Child:

enter code [BsonRef("pixel")]
public class Pixel : IPixel
{
    [BsonId]
    public Guid Id { get; set; }

    /// <summary>
    /// the X coordinate of the pixel
    /// </summary>
    public int X { get; set; }

    /// <summary>
    /// the Y coordinate of the pixel
    /// </summary>
    public int Y { get; set; }

    /// <summary>
    /// the color of the pixel
    /// </summary>
    public Color Color { get; set; }

    /// <summary>
    /// defines if the pixel was set from a text or a graphic object
    /// </summary>
    public PixelSource PixelSource { get; set; }
}

I tried tho map these collections with the BsonMapper:

   public void InsertPictogram(Pictogram pic)
    {
        var mapper = BsonMapper.Global;

        mapper.Entity<Pictogram>()
            .DbRef(p => p.Pixels, "pixel");

        if (pic == null) throw new ArgumentNullException(nameof(pic));
        using (var db = new LiteDatabase(_connectionString))
        {
            var pictograms = db.GetCollection<Pictogram>("pictogram");
            var pixels = db.GetCollection<IPixel>("pixel");


            foreach (var pixel in pic.Pixels.ToList())
            {
                pixels.Insert(pixel);
            }

            pictograms.Insert(pic);

        }
    }

But I'm getting a nullReferenceException when I try the insert the pic.

Can someone explain me how to work with list of list in LiteDB correctly?

Thanks a lot! BR Steff

1 Answers1

0

The pictograms is not null at insert the pic -> pictograms.Insert(pic);

It is an instance of LiteCollection:

pictograms={LiteDB.LiteCollection<PiktogrammManager.Model.Pictogram>}
Name="pictogram"
Visitor={LiteDB.QueryVisitor<PiktogrammManager.Model.Pictogram>}
_autoId=Guid
_engine={LiteDB.LiteEngine}
_id={LiteDB.MemberMapper}
_includes=Count = 0
_log={LiteDB.Logger}
_mapper={LiteDB.BsonMapper}
_name="pictogram"
_visitor={LiteDB.QueryVisitor<PiktogrammManager.Model.Pictogram>}