Melvin Caterpillar
Melvin Caterpillar

Reputation: 11

Storing a list in SQL within Xamarin

I have a custom class that I've created for a User object. Within this user object it has a property that's a List. When trying to store it within the SQLite database on my app it says it doesn't support storing Lists. Is there any way to be able to permanently store Lists of generic types in Xamarin so when a user logs in they can see the list associated with their account? Code below.

User class.

    public class User
    {
        [PrimaryKey, AutoIncrement, Unique]
        public int ID { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public string AuthToken { get; set; }
        public List<HabytItem> Habyts { get; set; } //Want to store this list in a database.
    }

Thanks for any help!

Upvotes: 0

Views: 151

Answers (1)

nevermore
nevermore

Reputation: 15816

You can open the readme documentation in the previous version here: sqlite-net-extensions, there has an example that shows you how to store a list in SQL with SQLite-Net Extensions version:

With SQLite-Net extensions, no more need to write the queries manually, just specify the relationships in the entities:

public class Stock
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    [MaxLength(8)]
    public string Symbol { get; set; }

    [OneToMany(CascadeOperations = CascadeOperation.All)]      // One to many relationship with Valuation
    public List<Valuation> Valuations { get; set; }
}

public class Valuation
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }

    [ForeignKey(typeof(Stock))]     // Specify the foreign key
    public int StockId { get; set; }
    public DateTime Time { get; set; }
    public decimal Price { get; set; }

    [ManyToOne]      // Many to one relationship with Stock
    public Stock Stock { get; set; }
}

There are also Read and write operations in the document you can refer.

Upvotes: 1

Related Questions