Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
110 views
in Technique[技术] by (71.8m points)

c# - Can I use a List of String in a class intended for SQLite?

What limitations are there on data types used in a class that will be used by SQLite-net to represent a table? Specifically, can I use this:

public List<string> CoconutWaterBrands { get; set; }

...or will I need this:

public string[] CoconutWaterBrands { get; set; }

...or something else altogether?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Similar to Sandy's Answer, in terms of serializing/deserializing the list, you could use Text blobbed properties from the SQLite-Net Extensions library. So for example in your Model class:

public class SomethingToDoWithCoconuts
{
    [TextBlob(nameof(CoconutWaterBrandsBlobbed))]
    public List<string> CoconutWaterBrands { get; set; }
    public string CoconutWaterBrandsBlobbed { 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...