我使用以下方法在学生和班级之间建立了多对多的关系SQLite-net Extensions
:
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
{
[ForeignKey(typeof(Students))]
public int StudentFId { get; set; }
[ForeignKey(typeof(Classes))]
public int ClassFId { get; set; }
}
我以这种方式添加关系:
dbConnection.InsertOrReplace(new Students_Classes { StudentFId = sId, ClassFId = cId });
但是当我想删除关系时:
var validRelation = dbConnection.Find<Students_Classes>(x => x.StudentFId = sId && x.ClassFId = cId);
if (validRelation == null)
return;
dbConnection.Delete(validRelation);
我得到一个错误说cannot delete because it has no PK
。我可以让一个学生拥有他的所有课程,删除一个课程,然后用他的课程再次保存它,但可能会出现性能问题。
如何删除多对多关系中的关系?谢谢。