I am using async programming in my c# and I am trying to update data into the db on checking if the records exist else add new record
var existingStudent = await CheckIfStudentExists(student); // check if the Student with the StudentCode is already exist
if (existingStudent is null)
{
await _context.Students.AddAsync(student);
await SaveChanges();
}
else
{
student.StudentId = existingStudent.StudentId;
_context.Students.Update(student);
await SaveChanges();
}
Since here I am uploading bulk files (say around 7000) in one shot. It happens that the same StudentCode with 2 different files can comeup. Many times I have observed that ADD part of the code is executed without checking if the student exists.
What is the change I need to do so that If-Else condition is not executed till the CheckIfStudentExists
is executed.