Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework - Keep loaded/included related objects after 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
like image 413
SventoryMang Avatar asked Mar 24 '11 15:03

SventoryMang


2 Answers

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.

like image 120
Shiraz Bhaiji Avatar answered Oct 08 '22 23:10

Shiraz Bhaiji


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.

like image 23
bernhof Avatar answered Oct 08 '22 22:10

bernhof