public IActionResult Delete(int ActivityId) { if (HttpContext.Session.GetInt32("UserId") == null) { return(RedirectToAction("Index")); } User user = _context.Users.SingleOrDefault(u => u.UserId == HttpContext.Session.GetInt32("UserId")); Activity activity = _context.Activities.SingleOrDefault(a => a.ActivityId == ActivityId); List <Activitycenter> activities = _context.Activitycenters.Where(ac => ac.ActivityId == activity.ActivityId).ToList(); _context.Activitycenters.RemoveRange(activities); _context.SaveChanges(); _context.Remove(activity); _context.SaveChanges(); return(RedirectToAction("ActivityList")); }
private void EFbuttonDelete_Click(object sender, EventArgs e) { // Delete a blog entry try { if (int.TryParse(TextBoxBlogId.Text, out int id)) { using var dbContext = new myContext(); var blog = dbContext.Blogs.First <Blog>(b => b.Id == id); dbContext.Remove(blog); dbContext.SaveChanges(); Console.WriteLine($"Deleted blog with id={id}"); } } catch (Exception ex) { Console.WriteLine(ex); } }