sqlite net - Many to Many relationships with look up tables -


public class person {     [primarykey, autoincrement]     public int id { get; set; }     public string firstname { get; set; }     public string lastname { get; set; }      [manytomany(typeof(personcolor), cascadeoperations = cascadeoperation.all)]     public list<color> favoritecolors { get; set; }  }  public class color {     [primarykey, autoincrement]     public int id { get; set; }     public string name { get; set; }      [manytomany(typeof(personcolor))]     public list<person> people { get; set; } }  public class personcolor {     [foreignkey(typeof(person))]     public int personid { get; set; }      [foreignkey(typeof(color))]     public int colorid { get; set; } } 

...

var person = new person() {     firstname = "adrian",     lastname = "simbulan",     favoritecolors = new list<color>() {         new color() {name = "red"},         new color() {name = "green"}     } };  await _db.insertwithchildrenasync(person); 

ok i'm trying establish many many relationship between person , color. color table pre-populated static data.

now problem is, whenever execute "insertwithchildrenasync" command, inserts new data color lookup table. there way insert person record selected colors out affecting color table?

try removing write cascade operation favoritecolors attribute:

[manytomany(typeof(personcolor), cascadeoperations = cascadeoperation.cascaderead)] public list<color> favoritecolors { get; set; } 

this way library won't perform recursive write operations on table.


another way without modifying relationship performing two-step operation. first inserting object , updating relationship:

await _db.insertasync(person); await _db.updatewithchildrenasync(person); 

in both cases the objects in favoritecolors list should exist in database , should have valid primary key assigned. according this, sample code never work because identifier 0 in color objects.