实体框架:访问关联
本文关键字:关联 访问 框架 实体 | 更新日期: 2023-09-27 18:36:20
我的关联域模型如下:
-
Project
有很多Task
-
Task
有很多User
根据User
的状态,我需要更新项目中的字段。我的查询如下:
var projects = from project in _data.projects
join task in _data.tasks on project.ProjectID equals task.ProjectID
join user in _data.usersactive on task.TaskID equals user.TaskID
where user.Active == false
select project;
这允许我获取具有非活动用户的项目。
foreach (Project project in projects)
{
bool needUpdate = false;
foreach (Task task in project.tasks)
{
foreach (User user in task.users)
{
// Depending on state of user
// needUpdate = true;
}
}
// If needUpdate = true
// Do something
}
显然,这段代码会让我:
There is already an open DataReader associated with this Connection which must be closed first.
但是,我在MySQL上,因此无法简单地利用MultipleActiveResultSets=True
。
我如何能够急切地加载这些关联并在foreach
循环中访问它们?
急切地加载 linq 集:
var projects = (from project in _data.projects
join task in
_data.tasks on project.ProjectID equals task.ProjectID
join user in
_data.usersactive on task.TaskID equals user.TaskID
where user.Active == false
select project).ToList();