Removing / adding from memory usage of virtual collections
From what I can tell when profiling sql, it executes an sql statement to fetch all users for a group when called group.Users.Remove(user)
. Users are a virtual collection in a group, using Entity Framework 6 code first.
var usersToRemove = Context.users.Where(u=>someListOfIds.Contains(u.Id)); //might only be a few users
foreach(var group in Context.groups)
{
foreach(var user in usersToRemove.Where(u=>u.groupId == group.Id)){
group.Users.Remove(user);//group.Users might have millions of users, do all users get pulled back into memory at this point?
}
}
My question is, are all Users groups loaded into memory when deleted and an item from the collection here group.Users.Remove(user)
?
source to share
do all users. Groups are loaded into memory when removed and an item from the collection is here user.Groups.Remove (group)?
Yes, I think the call group.Users
will load all users. The call context.Users
(or context.Groups
) returns IQueryable
(because Users
and Groups
are DbSets
) when it is groups.Users
supposed to be ICollection<User>
. This way (if you have lazy loading enabled, etc.), it will be populated when it is accessed.
source to share