0

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

1 Answers1

0

if i understand right you want to perform the select and the delete in one query

i think this should help you Entity Framework Plus Batch Delete

Manuel Hess
  • 140
  • 9