更新数据与LinQ到SQL不工作

本文关键字:SQL 工作 LinQ 数据 更新 | 更新日期: 2023-09-27 17:50:57

我的数据库表是学生,按下更新按钮后无法更新。数据还是一样的

 string name = txtName.Text;
 string dob = txtDOB.Text;
 string tel = txtTelephone.Text;
 string address = txtAddress.Text;
 string email = User.Identity.Name;
 Student stud = db.Students.Single(u => u.StudentEmail == email);
 stud.Name = name;
 stud.DateOfBirth = dob;
 stud.Telephone = tel;
 stud.Address = address;
 db.SubmitChanges();
 Response.Redirect("Home.aspx");

更新数据与LinQ到SQL不工作

尝试设置"SubmitChanges"(SaveChanges)之前的修改状态:

db.Entry(stud).State = EntityState.Modified;

检查表是否有Primary Key,如果没有,LINQ to SQL将无法制定所需的UPDATE语句