我正在使用 Linq to Entity MVC,当我尝试从数据库中删除记录时,我得到了这个异常。
不允许新事务,因为会话中还有其他线程在运行。
我的代码:
if (Request.Form["Enroll"] != null)
{
string[] selected = Request.Form["Enroll"].Split(',');
if (selected != null)
{
if (selected.Count() != 0)
{
int k = 0;
foreach (var item in selected)
{
var TraineeId = Convert.ToInt32(item[k].ToString());
var sessionid = Convert.ToInt32(Session["user"].ToString());
var id = db.EnrollTrainee.Where(i => i.TraineeID == TraineeId
&& i.TrainerID == sessionid);
if (id != null)
{
foreach (var a in id)
{
//db.Database.Connection.Close();
EnrollTrainee delete = db.EnrollTrainee.Find(a.id);
db.EnrollTrainee.Remove(delete);
db.SaveChanges(); //Getting Exception Here
}
}
k++;
}
}
}
populatelistbox();
return View();
}
请帮忙。!!!提前致谢。!!!