Good day to all!
There is such a code
var userRoles = context.UserRoles.Where(ur => ur.UserId == id);
if (userRoles.Any())
{
context.UserRoles.RemoveRange(userRoles);
}
var userCars = context.UserCars.Where(uc => uc.UserId == id);
It is necessary to get rid of the extra SELECT code before deleting:
context.UserRoles.RemoveRange (userRoles);
How to do it better?
If RemoveRange loops through the incoming collection, then for each record in the UserCars collection, a separate query will be executed into the database, which is bad. The sample should be within the same request:
var userCars = context.UserCars.Where (uc => uc.UserId == id);
How to do it better?
Sincerely, Mikhail