实体框架 - 在结束使用后保留加载/包含的相关对象?

发布于 2024-10-26 02:38:54 字数 2757 浏览 2 评论 0原文

我正在尝试使用如下所示的方法进行查询:

Public Shared Function listParticipationsByTeamCount(ByVal count As Integer, ByVal challenge As Challenge) As List(Of Participation)
    Dim participationList As List(Of Participation)

    If count <> Nothing And challenge IsNot Nothing Then
        Using db As New DatabaseEntities()
            participationList = db.Participations.Where(Function(x) x.TeamCount = count And x.Challenge.Id = challenge.Id).OrderByDescending(Function(x) x.TeamCount).ThenBy(Function(x) x.Team.Name).ToList()
        End Using
    End If

    Return participationList
End Function

我有一个参与表,参与团队<之间存在多对一关系/em> 表,以及 ParticipationTeamMember 表之间的多对多关系。在我的标记页面上,当我像这样迭代列表时,我尝试类似的操作:

<% For Each participation As Participation In participationsList%>
    <tr>
        <td><a class="external-link" href="<%= participation.Team.Website %>"><%= participation.Team.Name%></a></td>
        <td><%= participation.Percentage%>%</td>
        <td>
            <%  For Each member As TeamMember In participation.TeamMembers%>
                <%= member.Name%><br /> 
            <% Next%>
        </td>
    </tr>
<% Next%>

我收到以下错误:

ObjectContext 实例已被释放,不能再用于需要连接的操作。

现在我明白了,这是因为我将查询放在 using 中,并且在 End using 之后我无法获取相关对象,在查找此内容后,我尝试将查询的 using 语句更改为

Using db As New DatabaseEntities()
        participationList = db.Participations.Include("Team").Include("TeamMember").Where(Function(x) x.TeamCount = count And x.Team.Id = team.Id).OrderByDescending(Function(x) x.TeamCount).ThenBy(Function(x) x.Team.Name).ToList()
End Using

:没用。我还尝试加载实体引用,如下所示:

Using db As New DatabaseEntities()
        participationList = db.Participations.Where(Function(x) x.TeamCount = count And x.Team.Id = team.Id).OrderByDescending(Function(x) x.TeamCount).ThenBy(Function(x) x.Team.Name).ToList()
    For each part as Participation in participationList
            part.TeamReference.Load()
            part.TeamMembers.Load()
    Next
End Using

错误仍然存​​在。如何将所有这些相关对象加载到我的参与列表中,以便在我结束使用后仍然可以引用它们???我知道 EF4 现在默认执行延迟加载,但即使我显式加载相关对象,它似乎仍然不起作用。

编辑:感谢所有的答案,这是一个不复数团队成员的问题,因为它是一个集合。这打破了整个查询。所以答案是:

Using db As New DatabaseEntities()
        participationList = db.Participations.Include("Team").Include("TeamMembers").Where(Function(x) x.TeamCount = count And x.Team.Id = team.Id).OrderByDescending(Function(x) x.TeamCount).ThenBy(Function(x) x.Team.Name).ToList()
End Using

I am trying to have a query in a method that looks like this:

Public Shared Function listParticipationsByTeamCount(ByVal count As Integer, ByVal challenge As Challenge) As List(Of Participation)
    Dim participationList As List(Of Participation)

    If count <> Nothing And challenge IsNot Nothing Then
        Using db As New DatabaseEntities()
            participationList = db.Participations.Where(Function(x) x.TeamCount = count And x.Challenge.Id = challenge.Id).OrderByDescending(Function(x) x.TeamCount).ThenBy(Function(x) x.Team.Name).ToList()
        End Using
    End If

    Return participationList
End Function

I have a Participation table, that has a many to 1 relationship between Participation and Team table, and a many to many relationship between Participation and TeamMember table. On my markup page I try something like this when I iterate through the list like so:

<% For Each participation As Participation In participationsList%>
    <tr>
        <td><a class="external-link" href="<%= participation.Team.Website %>"><%= participation.Team.Name%></a></td>
        <td><%= participation.Percentage%>%</td>
        <td>
            <%  For Each member As TeamMember In participation.TeamMembers%>
                <%= member.Name%><br /> 
            <% Next%>
        </td>
    </tr>
<% Next%>

I get the following error:

The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

Now I understand that is because I put the query inside a using and after the End Using I can't get the related objects, upon looking this up I tried changing the query the using statement to this:

Using db As New DatabaseEntities()
        participationList = db.Participations.Include("Team").Include("TeamMember").Where(Function(x) x.TeamCount = count And x.Team.Id = team.Id).OrderByDescending(Function(x) x.TeamCount).ThenBy(Function(x) x.Team.Name).ToList()
End Using

This didn't work. I also tried loading entity references like so:

Using db As New DatabaseEntities()
        participationList = db.Participations.Where(Function(x) x.TeamCount = count And x.Team.Id = team.Id).OrderByDescending(Function(x) x.TeamCount).ThenBy(Function(x) x.Team.Name).ToList()
    For each part as Participation in participationList
            part.TeamReference.Load()
            part.TeamMembers.Load()
    Next
End Using

The error still persisted. How can I load all these related objects into my participationList so I can reference them still after the I End Using??? I know EF4 now does lazyloading by default, but even when I explicitly load related objects it still doesn't seem to be working.

Edit: Thanks for all the answers all, it was a matter of not pluralizing the TeamMembers include, since it was a collection. This broke the whole query. So the answer is:

Using db As New DatabaseEntities()
        participationList = db.Participations.Include("Team").Include("TeamMembers").Where(Function(x) x.TeamCount = count And x.Team.Id = team.Id).OrderByDescending(Function(x) x.TeamCount).ThenBy(Function(x) x.Team.Name).ToList()
End Using

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

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

发布评论

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

评论(2

半窗疏影 2024-11-02 02:38:54

问题似乎是您正在设置查询但未执行它。

  • 只有在 using 语句中时,您才可以访问数据库。
  • 当您实际尝试使用数据时,请求将发送到数据库。

因此,在退出 using 语句之前,您应该循环遍历数据并将其放置在域对象中。

The problem appears to be that you are setting up the query but not executing it.

  • You only have access to the database when you are inside the using statement.
  • The request is sent to the database when you actually try to use the data.

So you should loop through your data and place it inside a domain object, before exiting the using statement.

书信已泛黄 2024-11-02 02:38:54

在第三个代码块中,您将包含 TeamMember。但是,从我在第二个代码块中看到的情况来看,您的导航属性是复数的,因此请尝试包含 TeamMembers

In the third code block you are including TeamMember. However, from what I can see in the second code block, your navigation properties are pluralized, so try including TeamMembers instead.

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