linq to entity报"不允许启动新事务,因为有其他线程正在该会话中运行."错误解决方法

时间:2022-05-25 23:38:07

using (var entity = new USERRIGHTEntities())
                {
                    int tCode = Convert.ToInt32(tPropertiesCode);
                    //var catetory = entity.T_ModelProperties.Where(o => o.F_ProperieCode == int.Parse(tPropertiesCode));
                    var catetory = entity.T_ModelProperties.Where(o => o.F_ProperieCode == tCode);
                    if (catetory.Any())
                    {
                        foreach (var item in catetory)
                        {
                            entity.DeleteObject(item);
                            //entity.SaveChanges();
                        }
                        entity.SaveChanges();
                    }
                }

可以把entity.SaveChanges();放到循环外边,就可以成功删除了,也可以成功删除多条数据。