asp.net mvc当你更新数据库中的一个项目时,你如何更新数据库(数据库更新不正确)



我有这个功能,它可以更新学生(用户(中的某些信息

public static bool UpdateStudent(this DBEntities1 DB, StudentView studentView)
{
Student studentToUpdate = DB.Students.Find(studentView.idStudent);
studentView.CopyToStudent(studentToUpdate);
DB.Entry(studentToUpdate).State = EntityState.Modified;
DB.SaveChanges();
return true;
}

当学生更改自己的信息时,没有问题,数据库更新也没有问题。问题是,当管理员更改信息(停用帐户(时,它会更改数据库中的值,但仅限于管理员。从学生的角度来看,他不受更改的影响,但管理员认为他已停用,当我检查数据库时,该学生被视为已停用。这就像当学生登录时,他会使用数据库中以前的旧值,而不是新值。这是停用学生的功能:

public static bool DeactivateStudent(this DBEntities1 DB, StudentView studentView)
{
OnlineStudents.RemoveSessionStudent(studentView); //tried to see if it was taking the student from the online session
studentView.active = false;
BeginTransaction(DB);
if (studentView.admitted) //delete rep if accepted
{
DB.Representatives.Remove(DB.FindRepbyStudentId(studentView.idStudent));
studentView.admitted = false;
}
DB.UpdateStudent(studentView);
AddDeletedStudent(DB, studentView); //add deleted student

if (InscriptionExist(DB, studentView.idStudent)) //delete inscription
DeleteStudentInscription(DB, studentView.idStudent);
DB.WriteStudentLog(studentView.idStudent, LogActions.deleteAccount);
DB.SaveChanges();
Commit();
return true;
}

我不明白为什么一方面它采用了新的数据库值,另一方面却没有。当我关闭应用程序并重新启动它时,学生就会获得更新的新信息。

在第一种情况下,您使用DB.Students.Find(studentView.idStudent(查找要更新的学生,使用studentView.CopyToStudent方法,您可能只更新所需的数据(从而防止成本过高(,然后保存更改。

第二个程序对我来说似乎不太清楚。在第二种情况下,尝试使用相同的程序,只是只更新已允许的标志。

也许您还可以考虑处理并发冲突。

最新更新