如何在 SQLite.net 中存储对象列表?

How can you store lists of objects in SQLite.net?

让我们假设我有这两个对象

class Customer {
   [PrimaryKey]
   public string id;
   [??????]
   public List<int> addresses;
}

class Address {
     [PrimaryKey, AutoIncrement]
     public int id;
     public string street;
     public int number;
}

有没有办法使用 SQLite.NET ORM 来保存 Customers 对象?因为我很难保存列表。

如果没有这样的方法,是否有某种我可以实现的事件或我可以覆盖的方法,以便在加载对象时触发代码?

我正在考虑在地址列表上方添加 [Ignore],当事件触发时我可以使用 SQLite.net 从另一个 table[加载地址的 ID] =13=]

在此先感谢您提供的任何帮助

看看这个答案here

您可以使用 SQLite-Net Extensions library

中的文本斑点属性来实现

例如在您的模型中 class:

public class Customer 
{
   [PrimaryKey]
   public string id;
   [TextBlob("addressesBlobbed")]
   public List<int> addresses { get; set; }
   public string addressesBlobbed { get; set; } // serialized CoconutWaterBrands
}

来自文本斑点属性的文档:

Text-blobbed properties are serialized into a text property when saved and deserialized when loaded. This allows storing simple objects in the same table in a single column.

Text-blobbed properties have a small overhead of serializing and deserializing the objects and some limitations, but are the best way to store simple objects like List or Dictionary of basic types or simple relationships. Text-blobbed properties require a declared string property where the serialized object is stored.

Text-blobbed properties cannot have relationships to other objects nor inverse relationship to its parent.

A JSON-based serializer is used if no other serializer has been specified using TextBlobOperations.SetTextSerializer method. To use the JSON serializer, a reference to Newtonsoft Json.Net library must be included in the project, also available as a NuGet package.

希望这对您有所帮助。