0

I have tow table in MySql

Students:

sCode sName depart

Grades

gID grade sCode

I want to delete students and related grades based on Depart parameter.

I'm using Delete Depart Action in my studentContrller:

public ActionResult DeleteDepartConfirmd(string Depart)
    {

        var codes = db.students.Where(d => d.Depart == Depart).Select(s => s.sCode);
        var grades = db.grades.Where(s => codes.Contains(s.sCode));
        db.grades.RemoveRange(grades);
        db.SaveChanges();

        var students = db.students.Where(d => d.Depart == Depart);
        db.students.RemoveRange(students);
        db.SaveChanges();
        return RedirectToAction("studentsByDepart");
    }

The problem occourd in first stage in deleting records in Grade table. The process delete a few records (3-10) records and Time Out happened in browser as it takes a long time. My question: is my deleting method is the problem? if yes how can I make it better?

The problem occurs in db.SaveChanges();

Note: I'm using Google cloud as MySql Host

khorshid
  • 59
  • 2
  • 10

1 Answers1

0

Try increasing timeout of your mySQL.

Check out this similar thread: How can I change the default Mysql connection timeout when connecting through python?

Community
  • 1
  • 1
Tony S
  • 156
  • 1
  • 2
  • 15
  • Thanks, but I want to delete more than 1000 records. and increasing time will not solve the problem. my be there is a way to bulk deleting. – khorshid Jul 26 '16 at 05:32
  • Try deleting using foreach loop. foreach(var agShare in agShares) { context.SecurityDataShares.Remove(agShare); } context.SaveChanges(); – Tony S Jul 26 '16 at 09:35