public static void Delete(int?id) { using (Entities db = new Entities()) { using (var transaction = db.Database.BeginTransaction()) { try { List List = db.List.Find(id); List <Task> tasks = TasksBLL.List(); foreach (var item in tasks) { if (item.id_list == List.id_list) { TasksBLL.Delete(item.id_task); } } db.Entry(List).State = System.Data.Entity.EntityState.Deleted; db.SaveChanges(); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw ex; } } } }