1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// MVC Controller for deleting Parent(s) in "Child".

// POST: Assignments/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
var child = await _context.Children.FindAsync(id);
if (child != null)
{
_context.Children.Remove(child);

/* Additionally process deleting data relating by Foreign Key. */
// Find out all the items, and collect them into a list.
/* Parent 1 */
var parent = _context.Parents.Where(e => e.PId == id).ToList();
if (parent != null)
{
foreach (var p in parent)
{
_context.Parents.Remove(p);
}
}

/* Parent 2 */
//.
//.
//.
}

//Console.WriteLine(_context.ChangeTracker.DebugView.LongView);

await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}