4

I'm trying to delete records from a database using SQLite/C# in Visual Studio 2012. Whenever I attempt to delete a record I get the following error:

SQLite Error: Cannot delete WhereListIterator`1: it has no PK

I am wanting to delete records using two different methods. Either delete using only the EID, which is the primary key, or delete records where the user provides everything but the EID.

if (EID_TextBox.Text != String.Empty) {
    if (Roster_Enrollment.Any(x => x.EID.Equals(Int32.Parse(EID_TextBox.Text)))) {
        App.DBConnection.Delete(Roster_Enrollment.Where(x => x.EID.Equals(Int32.Parse(EID_TextBox.Text))));
        Message_TextBlock.Text = "Enrollment deleted.";
    } else
        Message_TextBlock.Text = "Enrollment not found.";
}
else if (Roster_Enrollment.Any(x => x.CourseNo.Equals(CourseNo_TextBox.Text) && x.SectionNo.Equals(SectionNo_TextBox.Text) && x.Tno.Equals(Tno_TextBox.Text)))
    App.DBConnection.Delete(Roster_Enrollment.Where(x => x.CourseNo.Equals(CourseNo_TextBox.Text) && x.SectionNo.Equals(SectionNo_TextBox.Text) && x.Tno.Equals(Tno_TextBox.Text)));
else
    Message_TextBlock.Text = "Enrollment not found.";
Falko
  • 17,076
  • 13
  • 60
  • 105
cdalto
  • 797
  • 1
  • 15
  • 33

2 Answers2

10

Does your class data model have the Table attribute and PrimaryKey attribute?

[Table("StuffDataModel")]
public class StuffDataModel
{
    [PrimaryKey]
    public int Id { get; set; }

    public byte[] Stuff { get; set; }

}
Falko
  • 17,076
  • 13
  • 60
  • 105
Gary Davies
  • 920
  • 15
  • 12
  • 3
    Note that the primary key need's to have a *public setter*. This detail just cost me an hour of debugging. – Falko Feb 03 '18 at 18:07
  • I had set my key with [Key} which compiled and ran but still got the Sqlite error. So I changed that to [Primary Key] and visual studio had me add using SQLite; and the delete worked. Also I dropped the table and let my app recreate it. Not sure if that was required. – pdschuller Jul 22 '22 at 15:14
2

I see were the problem is... The Delete method takes an object as argument but you are using a query in argument. Try this instead: App.dbconnection.Table().Delete(x => x.EID.Equals(Int32.Parse(EID_TextBox.Text)));