Cascading delete removes dependent records when their principal record is deleted. In an Entity Framework Core API, deleting a Book can therefore remove its related Ratings. section 5.5
EF Core assumes this relationship should cascade, but a simple entity lookup may not translate the dependent deletion into SQL. The database then rejects the delete because Ratings still reference the Book through foreign keys. section 5.5
Make the dependent relationship available before removing the principal:
- Load the Book together with its Ratings. section 5.5
- Remove the Book and call SaveChangesAsync. section 5.5
- Return an appropriate HTTP response, such as 404 when the Book does not exist and 204 after deletion. section 5.5
Load dependents before deletion
var book = await _dbContext.Books .Include(b => b.Ratings) .FirstOrDefaultAsync(b => b.Id == id, cancellationToken); section 5.5


