C# asp.net mvc在更新数据库中的项目(数据库未正确更新)时,如何更新数据库?

C# asp.net mvc在更新数据库中的项目(数据库未正确更新)时,如何更新数据库?,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,我有一个更新学生(用户)某些信息的功能 当学生更改自己的信息时,没有问题,数据库更新没有问题。问题是,当管理员更改信息(停用帐户)时,它会更改数据库中的值,但只会更改管理员的值。从学生方面看,他不受更改的影响,但管理员将他视为已停用,当我检查数据库时,该学生将被视为已停用。这就像当学生登录时,他使用数据库中以前的旧值,而不是新值。 这是停用学员的功能: public static bool DeactivateStudent(this DBEntities1 DB, StudentView st

我有一个更新学生(用户)某些信息的功能

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

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方法,您可能只会更新所需的数据(从而防止成本过高)然后保存更改

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


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

嗯,我在第二个函数中使用了相同的过程,它只是额外的步骤,因为当我停用一个学生时,我改变了数据库中的其他东西。只是当学生连接到数据库时,除了从学生的角度之外,所有的东西都被更新了。
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;
        }