如何在sqlite . net MAUI上存储字典



在我的应用程序中,每个对象都有一个映射DateTime和String的字典。我正在使用sqlite-net-pcl和SQLiteNetExtensions,我不知道如何或是否有可能在数据库上存储字典。

这是我的类:

public class Addiction
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Name { get; set; }
public DateTime CreationDate { get; set; }
public DateTime LastResetDate { get; set; }
public Dictionary<DateTime, string> Notes { get; set; }
}

我试图使用文本blobs属性,但我不知道如何在类中实现以及如何使用后插入对象。也许还有其他方法,或者使用其他类型的数据库或本地存储方法。

您应该规范化您的数据库,并在关系1-n (1 addiction, n Notes)中创建另一个表,这样您就可以有一个笔记的List而不是字典:

public class Addiction
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Name { get; set; }
public DateTime CreationDate { get; set; }
public DateTime LastResetDate { get; set; }
public List<AddictionNotes> Notes { get; set; }
}

你应该有两个表:AddictionAddictionNotes

Addiction与您的属性(没有注释)和AddictionNotes像这个类:

[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public int AddictionId { get; set; }
public DateTime datetimeNote { get; set; }
public string Note { get; set; }

你也可以有一个Dictionary的AddictionNotes,但List更容易处理。

我也会建议使用实体框架从EF SQLite nuget包

最新更新