簡體   English   中英

如何在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對象? 因為我很難保存列表。

如果沒有這種方式,是否有某種我可以實現的事件或我可以覆蓋的方法,以便當一個對象被加載時代碼會觸發?

我正在考慮在地址列表上方添加[忽略]的方法,當事件觸發時我可以使用SQLite.net從另一個表加載地址的ID

提前感謝您提供的任何幫助

這里看看這個答案

您可以使用SQLite-Net Extensions庫中的 Text blobbed屬性來執行此操作

例如,在您的Model類中:

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

從文本blobbed屬性的文檔:

文本 - blobbed屬性在加載時被序列化為文本屬性,並在加載時反序列化。 這允許在單個列中將簡單對象存儲在同一個表中。

文本blobbed屬性具有序列化和反序列化對象的一小部分開銷和一些限制,但是存儲基本類型的List或Dictionary等簡單對象或簡單關系的最佳方法。 Text-blobbed屬性需要一個聲明的字符串屬性,其中存儲了序列化對象。

文本blobbed屬性不能與其他對象建立關系,也不能與其父對象建立反向關系。

如果未使用TextBlobOperations.SetTextSerializer方法指定其他序列化程序,則使用基於JSON的序列化程序。 要使用JSON序列化程序,必須在項目中包含對Newtonsoft Json.Net庫的引用,也可以作為NuGet包使用。

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM