实体框架 - 跳过导航路径中的对象

发布于 2024-12-11 00:51:43 字数 628 浏览 0 评论 0原文

我有三个基于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稍尽春風 2024-12-18 00:51:43

如果我的查询正确的话,加载所需数量的没有包含的 Puples 只是在 1 个表上进行选择,而不进行分段或排序。那当然更快了。

现在您加载所有导航属性。这就是一些工作。您是否想过只选择瞳孔及其评分因子?

var pupils = from pupil in db.Pupils 
             select new
             {
                Pupil = pupil,
                GradingFactor = pupil.SchoolClass.ClassType.GradingFactor
             }; 

另一个优化是将 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?

var pupils = from pupil in db.Pupils 
             select new
             {
                Pupil = pupil,
                GradingFactor = pupil.SchoolClass.ClassType.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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文