我有类似的情况,对于这个post删除有效但仅在学生和类表上,在student_classes表中元素不会被删除。
public class Students
{
[PrimaryKey, AutoIncrement]
public int StudentId { get; set; }
public string Name { get; set; }
[ManyToMany(typeof(Students_Classes))]
public List<Classes> Classes { get; set; }
}
public class Classes
{
[PrimaryKey, AutoIncrement]
public int ClassId { get; set; }
public string Name { get; set; }
[ManyToMany(typeof(Students_Classes))]
public List<Students> Students { get; set; }
}
public class Students_Classes
{
[PrimaryKey, AutoIncrement]
public int StudentClassesId { get; set; }
[ForeignKey(typeof(Students))]
public int StudentFId { get; set; }
[ForeignKey(typeof(Classes))]
public int ClassFId { get; set; }
}
要删除我使用以下代码
conn.Delete(student, true);
插入和更新期间我没有问题 感谢
P.S。我的课程名称不同,但并不重要,代码真的相同
答案 0 :(得分:1)
您的Students_Classes类中没有主键。这就是问题所在。
答案 1 :(得分:0)
级联删除doesn't remove intermediate records。这些记录是无害的,但完全没用,您可以手动删除它们。例如:
// Fetch IDs of intermediate records to delete
var deleteIds = conn.Table<Student_Classes>()
.Where(e => e.StudentFId == student.StudentId)
.Select(e => e.StudentFId).ToList();
// Perform batch deletion
conn.DeleteAllIds<Student_Classes>(deleteIds);
或者性能更高(但重构程度更低):
var deleteQuery = "DELETE FROM Student_Classes WHERE StudentFId == ?";
conn.Execute(deleteQuery, student.StudentId);