【发布时间】:2014-10-16 19:31:11
【问题描述】:
我使用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。我可以获取一个学生的所有课程,删除一个课程,然后将其与他的课程一起保存,但可能会出现性能问题。
如何删除多对多关系中的关系?谢谢。
【问题讨论】:
标签: c# sql sqlite sqlite-net sqlite-net-extensions