【发布时间】:2016-03-13 20:12:46
【问题描述】:
我正在构建一个 Xamarin 移动应用程序,并且我有一组从 rest api 获得的产品。每个产品可以有 n 个图像。我已经使用 SQLite-Net Extensions 设置了表的关系。但是,当我将值插入数据库时,Images 字段在 Product 表中为空值,而 Image 表为空。
我的模特
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;
}
如果有人能帮助我找出我做错了什么,那就太好了。
【问题讨论】:
标签: c# sqlite xamarin sqlite-net sqlite-net-extensions