Please pardon the wall of code, but I need to set the stage....
public class Student
{
public string Name { get; set; }
[PrimaryKey]
public string Id { get; set; }
[ManyToMany(typeof(StudentStaff))]
public List<Staff> Teachers { get; set; }
[ManyToMany(typeof(StudentGuardian))]
public List<Guardian> Guardians { get; set; }
}
public class Guardian
{
[PrimaryKey]
public string Id { get; set; }
public string Name{get;set;}
[ManyToMany(typeof(StudentGuardian))]
public List<Student> Students { get; set; }
}
public class Staff
{
[PrimaryKey]
public string Id { get; set; }
public string Name { get; set; }
[ManyToMany(typeof(StudentStaff))]
public List<Student> Students { get; set; }
}
public class StudentStaff
{
[ForeignKey(typeof(Student))]
public string StudentId { get; set; }
[ForeignKey(typeof(Staff))]
public string StaffId { get; set; }
}
public class StudentGuardian
{
[ForeignKey(typeof(Student))]
public string StudentId { get; set; }
[ForeignKey(typeof(Guardian))]
public string GuardianId { get; set; }
}
Given those classes how should I insert the Students, Staff and Guardians into the database while maintaining the relationships? I should note that the server returns populated Student records, so that is my starting point.
I tried conn.InsertOrReplaceWithChidlren(student);
...that inserted the student, the studentstaff and the studentguardian records but not the actual staff or guardian.
I tried
conn.InsertOrReplaceWithChildren(student);
conn.InsertOrReplaceWithChildren(student.Teachers);
conn.InsertOrReplaceWithChildren(student.Guardians);
Oddly that ended up with staff, guardian and teachers inserted but neither of the intersection tables had any data...
--Update---
I just tried
conn.InsertOrReplaceWithChildren(student.Teachers);
conn.InsertOrReplaceWithChildren(student.Guardians);
conn.InsertOrReplaceWithChildren(student);
And it worked perfectly...the question is why? Why does a many to many seem to be dependent on operation order?