c# - 从虚拟集合中删除/添加内存使用

标签 c# ef-code-first out-of-memory entity-framework-6

据我分析 sql 时所知,它会在调用 group.Users.Remove(user) 时执行一条 sql 语句来检索该组的所有用户。 Users 是组上的虚拟集合,首先使用 Entity Framework 6 代码。

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?
    }
}

我的问题是,当从此处 group.Users.Remove(user) 的集合中删除和项目时,是否所有用户组都加载到内存中?

最佳答案

do all the users.Groups get loaded into memory when removing and item from the collection here user.Groups.Remove(group)?

是的,我认为调用 group.Users 加载所有用户。正在调用 context.Users (或 context.Groups )将返回 IQueryable (因为 UsersGroupsDbSets ),而 groups.Users大概是 ICollection<User> .因此(假设您启用了延迟加载等)它会在您访问时填充。

关于c# - 从虚拟集合中删除/添加内存使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30264991/

相关文章:

c# - 具有多个模板的 ASP Repeater 控件

c# - 在 C# 中构造树算法的最佳方法

asp.net-membership - EF 4.1 Update 1 Code First + ASP.NET 成员资格提供程序

c# - Entity Framework 4.1 - EFTracingProvider

java - 关于 OutOfMemoryError 及其处理方法

c# - 使用多个相关的可终结对象实现可终结处理模式

c# - 无法使 EF Code First 使用手动更改的数据库

c++ - 如何在禁用 C++ 异常的情况下确保内存不足的健壮性(VS2010)?

c++ - 堆上的内存是如何耗尽的?

c# - 如何将 MVC View 中的下拉列表选定值发送到 Controller 的变量 ViewData?