请原谅代码墙,但我需要设置舞台......
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; }
}
鉴于这些课程,我应该如何在保持关系的同时将学生、员工和监护人插入数据库?我应该注意服务器返回填充的学生记录,所以这是我的起点。
我试过conn.InsertOrReplaceWithChidlren(student);
...插入学生、学生工作人员和学生监护人记录,但没有插入实际工作人员或监护人。我尝试
conn.InsertOrReplaceWithChildren(student);
conn.InsertOrReplaceWithChildren(student.Teachers);
conn.InsertOrReplaceWithChildren(student.Guardians);
了 Oddly,最终插入了工作人员、监护人和教师,但两个交叉表都没有任何数据......
--更新---我刚试过
conn.InsertOrReplaceWithChildren(student.Teachers);
conn.InsertOrReplaceWithChildren(student.Guardians);
conn.InsertOrReplaceWithChildren(student);
而且效果很好......问题是为什么?为什么多对多似乎依赖于操作顺序?