0

我正在使用 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();
}

请帮忙。!!!提前致谢。!!!

4

2 回答 2

3

就我而言,在嵌套循环中较少调用 SaveChanges() 可以解决问题:

//produces error 
foreach(...) {
    foreach(...) {
       ...
       db.SaveChanges();
    } }

这是我的解决方案

//does not produce error
foreach(...) {
    foreach(...) {
       ...
    }
}
db.SaveChanges();
于 2013-05-08T17:06:53.637 回答
1

在这个不错的博客中得到了一个很好的解决方案。

我的问题的解决方案:-

 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);
                                var idlist = id.ToArray<EnrollTrainee>();//Added New Line

                                if (idlist != null)
                                {
                                    foreach (var a in idlist)
                                    {
                                        EnrollTrainee delete = db.EnrollTrainee.Find(a.id);
                                        db.EnrollTrainee.Remove(delete);
                                        db.SaveChanges();                                           
                                    }
                                }
                                k++;
                            }
                        }
                    }
                    populatelistbox();
                    return View();
                }
于 2012-09-08T08:18:27.063 回答