实体框架 - 跳过导航路径中的对象
我有三个基于 3 个数据库表的实体(基数低于关系)。
Pupil - SchoolClass - ClassType
N:1 1:1
我想获取学生列表并更新他们的标量属性Grade
。 成绩取决于学生参加的班级类型。所以我还需要每个学生的 ClassType。
这就是我所做的:
var pupils = from pupil in db.Pupils.Include("SchoolClass.ClassType")
select pupil;
foreach(Pupil p in pupils)
p.Grade *= p.SchoolClass.ClassType.GradingFactor;
问题是 SchoolClass 被加载到上下文中,所有标量属性都已填充(+外键 ID)。
有没有办法跳过 Pupil 和 ClassType 实体之间的对象?也就是说,只加载 SchoolClass 的 ClassType 导航属性?
加载所需数量的学生(不使用 Include 时)为 20 毫秒,使用 Include 时为 250 毫秒。我想知道这是否可以优化?
I have three entities based on 3 DB tables, (cardinality below the relationships).
Pupil - SchoolClass - ClassType
N:1 1:1
I want to fetch a list of pupils and update their scalar property Grade
.
The Grade depends on the class type the pupil attends. So I also need the ClassType for each Pupil.
This is what I do:
var pupils = from pupil in db.Pupils.Include("SchoolClass.ClassType")
select pupil;
foreach(Pupil p in pupils)
p.Grade *= p.SchoolClass.ClassType.GradingFactor;
The problem with this is that the SchoolClass is loaded into the context, with all the scalar properties filled in (+ foreign key IDs).
Is there a way to skip an object in between the Pupil and the ClassType entities? That is, to load only SchoolClass' ClassType navigation property?
Loading desired number of pupils without Include was 20 ms, and with Include it was 250 ms. I am wondering if this can be optimized?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我的查询正确的话,加载所需数量的没有包含的 Puples 只是在 1 个表上进行选择,而不进行分段或排序。那当然更快了。
现在您加载所有导航属性。这就是一些工作。您是否想过只选择瞳孔及其评分因子?
另一个优化是将 SchoolClass 和 ClassType 合并在一起,因为它们的关系是 1:1(根据您的规范),为您节省 1 个连接
Loading the desired number of puples without an include was just a select on 1 table without segmentation or sorting if i get your query right. That ofcourse is faster.
Now you load in all navigation properties. Thats some work. Have you thought of only selecting the Pupil and its GradingFactor?
Another optimalisation would be to merge SchoolClass and ClassType together since their relationship is 1:1 (according to your specification) saving you 1 join