我正在构建一个 Xamarin 移动应用程序,并且我有一组从 rest api 获得的产品。每个产品可以有 n 个图像。我已经使用 SQLite-Net Extensions 设置了表的关系。但是,当我将值插入数据库时,图像字段在产品表中的值为空,而图像表为空。
我的模特
public class Product
{
[SQLite.Net.Attributes.PrimaryKeyAttribute]
public int ID { get; set; }
public string Name { get; set; }
[OneToMany]
public List<Image> Images { get; set; }
}
public class Image
{
[SQLite.Net.Attributes.PrimaryKeyAttribute, SQLite.Net.Attributes.AutoIncrement]
public int ID { get; set; }
[ForeignKey(typeof(Product))] // Specify the foreign key
public int ProductID { get; set; }
public string Link { get; set; }
}
我用来插入值的代码。
var db = new SQLiteConnection(new SQLitePlatformAndroid(), path);
var info = db.GetTableInfo("ProductDB");
db.CreateTable<ProductDB>();
db.CreateTable<ImageDB>();
db.DeleteAll<ProductDB>();
db.DeleteAll<ImageDB>();
db.InsertAllWithChildren(data);
db.UpdateAll(data);
这就是我将json反序列化为对象的方式
public async Task<List<ProductDB>> GetAllProducts()
{
var httpClient = GetHttpClient();
var response = await httpClient.GetAsync(ServiceEndPoints.GetFilmsUri).ConfigureAwait(false);
if (response.IsSuccessStatusCode)
{
var content = response.Content;
string jsonString = await content.ReadAsStringAsync().ConfigureAwait(false);
return JsonConvert.DeserializeObject<List<ProductDB>>(jsonString);
}
return null;
}
如果有人可以帮助我确定我做错了什么,那就太好了。