4

I'm trying to map a complex class in sqlite but something seems to be wrong because when I retrieve my objects the complex fields are empty. Shortly, my class is something similar:

 public class Contact
 {
    [PrimaryKey, AutoIncrement]
    public int Id { set; get; }
    public string Name { set; get; }
    public string Status { set; get; }

    [TextBlob("PhoneNumbersBlobbed")]
    public List<PhoneNumber> PhoneNumbers { set; get; }
    public string PhoneNumbersBlobbed { get; set; } // serialized PhoneNumbers

    [TextBlob("MailAddressesBlobbed")]
    public List<ContactEmail> MailAddresses { set; get; }
    public string MailAddressesBlobbed { get; set; } // serialized MailAddresses
 }

I obviously have a separated class for PhoneNumber:

     public class PhoneNumber
     {          
        public string Number { set; get; }
        public bool IsVerified { set; get; }
        public string AttrString { set; get; }  
    }

while ContactEmail class is a native class of WinPhone.

Well, after have created the tables in DB:

 connection.CreateTable<Contact.PhoneNumber>();
 connection.CreateTable<Contact>();

and have populated my List of Contact I save them in DB. I tried as saving one by one with:

connection.InsertWithChildren(contact,true);

as all the list togheter:

connection.InsertAllWithChildren(listOfContacts, true);

and I'm sure that the list is correctly populated before to save it. But when I read its values from sqlite:

var list = connection.Table<Contact>().ToList();
connection.UpdateAll(list ,true);

I can't see values in my PhoneNumbers and MailAddresses fields, while they are filled with serialized value in their blobbed fields.

In addiction, trying to manually deserialize the blobbed fields, I get an exception from Json.net (ie. doing in this way):

 foreach (Contact pc in list)
        {
            try
            {
                Contact.PhoneNumber[] numbs = JsonConvert.DeserializeObject<PhoneContact.PhoneNumber[]>(pc.PhoneNumbersBlobbed);
            }
            catch {
                ;
            }
        }

what am I wrong? Thank you

Caio
  • 61
  • 5

1 Answers1

2

In order to retrieve relationships from the database you have to use any of the WithChildren methods provided by SQLite-Net Extensions.

Try changing these lines:

var list = connection.Table<Contact>().ToList();
connection.UpdateAll(list ,true);

To this:

var list = connection.GetAllWithChildren<Contact>();

You won't need the PhoneNumber table either, because they are being serialized in the text property. So you can remove this line:

connection.CreateTable<Contact.PhoneNumber>();
redent84
  • 18,901
  • 4
  • 62
  • 85
  • Thank you, I just tried to read values with the GetAllWithChildren method, it throw an exception via Json Deserializer. The exception message is `Error converting value "+393487373383" to type Windows.ApplicationModel.Contacts.Contact ` – Caio Sep 16 '15 at 22:50
  • Sounds like you saved a phone number ("+393487373383") in the `PhoneNumbersBlobbed` property and then you tried to deserialize it as a JSON. You shouldn't ever use `PhoneNumbersBlobbed` property directly. – redent84 Sep 17 '15 at 09:25
  • No, I didn't used directly the blobbed field, I simply declared as required the complex object decored by its blobbed string field and saved all the list as writed in the first post. – Caio Sep 18 '15 at 07:23