使用表达式在 EF4 中预加载多对多?

发布于 2024-12-06 08:16:06 字数 741 浏览 0 评论 0原文

我有两个实体:GrmDeploymentAttemptGrmDeploymentStep。这些实体通过中间 POCO GrmDeploymentAttemptStep 建立多对多关系,其中包含有关实际多对多关系的附加信息。

我试图通过急切加载来加载所有步骤信息的尝试,所以现在我有以下代码:

        var attempt = _context.GrmDeploymentAttempts
                              .Where(x => x.Id == attemptId)
                              .Include(x => x.AttemptSteps)
                              .FirstOrDefault();

问题是急切加载中间表,但不急切加载步骤表。如何将 Include() 与表达式一起使用来预先加载我的 Step 实体?使用 Include(string) 方法,我可以执行 Include("AttemptSteps.Steps") 但我不确定如何使用表达式执行此操作。

请注意,我知道我可以加载 AttemptSteps 实体并立即加载,但在某些情况下我无法执行此操作,并且我一直想知道如何处理此问题。

I have two Entities: GrmDeploymentAttempt and GrmDeploymentStep. These entities have a many to many relationship through an intermediary POCO GrmDeploymentAttemptStep, which has additional information about the actual many-to-many relationship.

I am trying to load an attempt with all step information through eager loading, so right now I have the following code:

        var attempt = _context.GrmDeploymentAttempts
                              .Where(x => x.Id == attemptId)
                              .Include(x => x.AttemptSteps)
                              .FirstOrDefault();

The problem is this eager load the intermediary table, but doesn't eager load the Steps table. How can I use the Include() with expressions to eager load my Step entity? Using the Include(string) method I could do Include("AttemptSteps.Steps") but I am not sure how to do this with expressions.

As a note, I know i could instead load the AttemptSteps entity and eager load off of there, but there are some situations where I am unable to do this and I have been wondering how to handle this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

我爱人 2024-12-13 08:16:06
var attempt = _context.GrmDeploymentAttempts
    .Where(x => x.Id == attemptId)
    .Include(x => x.AttemptSteps.Select(a => a.Step))
    .FirstOrDefault();

Include(...Select(...)) 通常会加载子集合的导航属性。

var attempt = _context.GrmDeploymentAttempts
    .Where(x => x.Id == attemptId)
    .Include(x => x.AttemptSteps.Select(a => a.Step))
    .FirstOrDefault();

Include(...Select(...)) generally loads a navigation property of a child collection.

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