Entity Framework - keep loaded / included related objects after end of use?

I am trying to get a request 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 in which there are many relationships between the Participation and Team tables and many relationships between the Participation and TeamMember. On my markup page, I try something like this when I repeat the list this way:

 <% 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 deleted and can no longer be used for operations that require a connection.

Now I understand that this is because I put the request in use, and after End Using I can not get the related objects, after looking at this, I tried to change the request: 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 

This did not work. I also tried loading object references this way:

 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 persists. How can I upload all of these related objects to my membership list so that I can refer to them even after I use End? I know that EF4 now does lazyloading by default, but even when I explicitly load related objects, it still doesn't work.

Edit: Thanks for all the answers, this is not a multiple inclusion of TeamMembers, as it was a collection. This violated the entire request. 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 
+6
entity-framework entity-relationship entity-framework-4
source share
2 answers

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

+2
source share

The problem is that you are tuning the request but not executing it.

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

So, before you exit the using statement, you must skip your data and place it inside the domain object.

+3
source share

All Articles