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

后端 未结 1 1923
迷失自我
迷失自我 2020-12-19 00:52

Let us assume I have these two objects

class Customer {
   [PrimaryKey]
   public string id;
   [??????]
   public List addresses;
}
相关标签:
1条回答
  • 2020-12-19 01:31

    Have a look at this answer here

    You can do it with Text blobbed properties from the SQLite-Net Extensions library

    So for example in your Model class:

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

    from the documentation on Text blobbed properties:

    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.

    Hope this helps.

    0 讨论(0)
提交回复
热议问题