我正在努力在我的LiteDB数据库中插入/更新带有对象列表的类的类。我尝试了几种方法和可能的解决方案,但无法使其正常工作。
这是我的课程:
父母:
[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; }
}
孩子:
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; }
}
我尝试通过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);
}
}
但是当我尝试插入图片时,我得到了nullReferenceException。
有人可以解释我如何正确使用LiteDB中的列表列表吗?
非常感谢! BR史蒂夫
答案 0 :(得分:0)
象形图在插入图片时不为空-> pictograms.Insert(pic);
它是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>}