在 sqlite-net 扩展中完全删除多对多 table 中的关系?

fully delete a relationship in a many to many table in sqlite-net extension?

我有类似的情况,对于这个 post 删除有效,但只对学生和 类 table,在 student_classes table 中元素不会被删除。

  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。我的类名字不同,不过没关系,代码真的一模一样

您的 Students_Classes class 中没有主键。就是这个问题。

级联删除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);