One to Many relationship in SQLite Xamarin
db.InsertAllWithChildren(data);db.UpdateAll(data);
You insert values with their child values, and then update with same values, with ProductID values being updated with null, as they should be set by database. If you want to update your data, your should set foreign key for each item being inserted, or use UpdateWithChildren
I guess that data
is a List<Product>
. In this case, you are only inserting products in the database and no images are being inserted.
You have two options here: insert images manually or use recursive operations.
To insert images manually, simply call InsertAll
method with all your images before inserting your products:
List<Image> images = data.SelectMany(p => p.Images ?? new List<Image>());conn.InsertAll(images);conn.InsertAllWithChildren(data);
Using recursive operations is simpler but requires minor configuration. Enable cascade operations by setting the CascadeOperation
property of the relationship attribute:
[OneToMany(CascadeOperations = CascadeOperation.All)] public List<Image> Images { get; set; }
Then, you can set the recursive
parameter of any -WithChildren
method to true
to perform the cascade operation:
conn.InsertAllWithChildren(data, recursive: true);