我正在努力将带有对象列表的类插入/更新到我的 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 中的列表列表吗?
非常感谢!布拉斯特夫