This might be a silly question but I am having trouble saving in sqlite. I have used facebook login from XF successfully, but also want to save the info from there in sqlite-net-pcl. Two models are:
public class FacebookProfile
{
public string Name { get; set; }
public string Locale { get; set; }
public string Link { get; set; }
[OneToOne]
[JsonProperty("age_range")]
public AgeRange AgeRange { get; set; }
[JsonProperty("first_name")]
public string FirstName { get; set; }
[JsonProperty("last_name")]
public string LastName { get; set; }
public string Gender { get; set; }
public bool IsVerified { get; set; }
[PrimaryKey]
public int Id { get; set; }
}
public class AgeRange
{
public int Min { get; set; }
}
I have successfully downloaded the info to my FacebookProfile model and can display it. But, when I am trying to save it in my sqlite database, AgeRange property of FacebookProfile saves as null in the database and it should have a certain value. The code for saving using sqlite connection is:
using (SQLite.SQLiteConnection conn = new SQLite.SQLiteConnection(App.DB_PATH))
{
conn.CreateTable<FacebookProfile>();
conn.Insert(fp); //fp is the FacebookProfile model info gotten from Facebook API and until this point, fp has agerange min value
}
After this point the agerange inserted in database is saved as null, I dont know why or I might be doing something wrong. So, when I try to retrieve this using following code in the viewmodel:
using (SQLite.SQLiteConnection conn = new SQLite.SQLiteConnection(App.DB_PATH))
{
conn.CreateTable<FacebookProfile>();
FacebookProfile = conn.Table<FacebookProfile>().FirstOrDefault();
}
The FacebookProfile retrieved from database has agerange value null but others I can get correct information. What might be the problem? Please help!
2
Answers
Sqlite-Net-pcl is not going to recognize custom poco objects such as:
Keep in mind, that Sqlite-net-pcl is basically just a wrapper that saves you the hassle of interacting directly with the Sqlite library. So, instead of having to do things like this:
You can simply say:
But, it doesn’t know what to do with your
AgeRange
class. Is that a string? Is, that an int? What is it as far as Sqlite is concerned? As others have said in the comments, the wrapper doesn’t know how to interpret that class into a database type such as STRING, NUMERIC etc.In general, I have had better success (when utilizing sqlite-net-pcl) creating an exact copy of a table structure as an intermediary class object and inserting/updating those via your parent class of FacebookProfile. This is referred to in programming patterns as the Anemic domain model where: “Anemic domain model is the use of a software domain model where the domain objects contain little or no business logic (validations, calculations, business rules etc.).”
In your case it may look something like this:
}
I think since both FacebookProfile and AgeRange can be said to be logically related you could mix these two into one table: