13

Let us assume I have these two objects

class Customer {
   [PrimaryKey]
   public string id;
   [??????]
   public List<int> addresses;
}

and

class Address {
     [PrimaryKey, AutoIncrement]
     public int id;
     public string street;
     public int number;
}

Is there a way to use the SQLite.NET ORM to save the Customers object? cause I'm having a really hard time saving lists.

If there is no such way, is there some sort of event I can implement or method I can override so that when an object gets loaded code will trigger?

I was thinking something along the lines of adding [Ignore] above the list of addresses and when the event triggers I can use SQLite.net to load the ids of the addresses from another table

Thanks in advance for any help you can provide

Cruces
  • 3,029
  • 1
  • 26
  • 55
  • 2
    I don't believe SQLite.NET natively supports this. You have to manually query the DB to populate that list based on the IDs of the two models. However, I've used the [SQLite Extensions](https://bitbucket.org/twincoders/sqlite-net-extensions) library, which does what you want. I had great success in a WPF project using the library... I don't know how well it will work in Xamarin, but I suppose it's worth a look. – Mage Xy Apr 25 '16 at 21:22
  • 1
    I use it with Xamarin and its fine, really great library! – Iain Smith Apr 25 '16 at 21:24

1 Answers1

17

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.

Community
  • 1
  • 1
Iain Smith
  • 9,230
  • 4
  • 50
  • 61
  • 1
    yeah, the text blobbed thing is not really what I was thinking of (initially my idea was to create a property that had a string of all the numbers seperated by | and having the setter split those numbers into a list, I didn't like that idea :) ) but the whole sqlite-net extensions thing seems to contain ways to have one to many relations so that seems to work find for me, thank you – Cruces Apr 25 '16 at 21:25
  • 1
    Yeah it can definitely do one to many relationships have a look at the Stocks/Valuation examples in the Docs in that link above – Iain Smith Apr 25 '16 at 21:27
  • yup that's what I was checking out, let's hope this works with xamarin :) I'll accept your answer in a few minutes, won't let me do it so soon after posting the question – Cruces Apr 25 '16 at 21:29
  • Ah cool no worries, defo works with xamarin made two apps with it. – Iain Smith Apr 25 '16 at 21:30
  • Ah, it was from the link I shared, but it is no joke... You should always be hydrated when storing objects in SQLite – Iain Smith Feb 11 '21 at 11:00