sqlite-net-extensions

With SQLite-Net Extensions, is there a way to have List<string>?


In the documentation for SQLite-Net Extensions, it shows having an object (Stock) with property that has a List of another object (Valuation) with a OneToMany relationship. I am able to reproduce that fine.

What I want to do is to be able to add a property to Stock that is a List of strings.

But when I try to add a List<string> property, if I have no attributes on it, I get the error: 'Don't know about System.Collections.Generic.List`1[System.String]'

If I add the [OneToMany] attribute to the property, I get the error: 'OneToMany relationships require Primary Key in the destination entity'

Is there any way that SQLite-Net Extensions can handle List<string> instead of List of non primative types?


Solution

  • Any kind of relationship requires a destination table. In your case, a string is not stored as a table. You can either convert that value into a complex object that is stored in a separate table and use either a OneToMany or a ManyToMany or serialize the list as a string and save it in a different field.

    For the latter case SQLite-Net Extensions includes a TextBlob attribute that does exactly that. It serializes the property as a string before storing the objech to database and deserializes it after reading from database.

    public class Person
    {
        public string Name { get; set; }
    
        [TextBlob("PhonesBlobbed")]
        public List<string> PhoneNumbers { get; set; }
    
        [TextBlob("AddressesBlobbed")]
        public List<Address> Addresses { get; set; }
    
        public string PhonesBlobbed { get; set; } // serialized phone numbers
        public string AddressesBlobbed { get; set; } // serialized addresses
    }
    

    More info in the manual.