This is a migrated thread and some comments may be shown as answers.

ObjectContext instance has been disposed using EF 4.0

4 Answers 407 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Bob
Top achievements
Rank 2
Bob asked on 03 May 2012, 11:28 PM
OK - So I'm still new to all this EF and Telerik stuff.  However after doing research I'm sure it's not a Telerik control issue.  In any case, I thought I would start here for help.
I have a simple Entity relationship (Lot_head and Lot_Detail)

When I execut the following code:
Private Sub btnSearchPO_Click(sender As System.Object, e As System.EventArgs) Handles btnSearchPO.Click
 
    LotDetailsDataGrid.DataSource = getLotDetails(PurchaseOrderTextBox.Text)
 
End Sub
 
Private Function getLotDetails(lotNumber As Integer) As List(Of Lot_Detail)
    Using context As New Three_Way_MatchEntities
        Dim lot = (From ld In context.Lot_Detail
        Where (ld.Lot_Number = lotNumber)
                  Select ld).ToList
 
        Return lot
    End Using
End Function

The grid populates the data correctly, however when I scroll to the end of a row, it throws the "The ObjectContext instance has been disposed and can no longer be used for operations that require a connection." exception.

Now I understand that since I am using lazing loading and using the 'Using' statement, the context has been disposed.  I'm also assuming that the datagrid must rely on having that context left open.  As many have mentioned you should keep the context open for only as long as you need it.  If that's the case, how would you write this example?

Thanks for the help!
Bob

4 Answers, 1 is accepted

Sort by
0
Bob
Top achievements
Rank 2
answered on 04 May 2012, 04:08 PM
OK - So I modified my code as shown below:
Private Sub btnSearchPO_Click(sender As System.Object, e As System.EventArgs) Handles btnSearchPO.Click
    getLotDetails(PurchaseOrderTextBox.Text)
End Sub
 
Private Sub getLotDetails(lotNumber As Integer)
    Dim lotDetails As ObjectQuery(Of Lot_Detail) = context.Lot_Detail
    Dim query = (From ld In lotDetails
                Where ld.Lot_Number = lotNumber
                Select ld.Lot_Number_Prefix, ld.Lot_Number, ld.Lot_Number_Suffix, ld.Book_Number, ld.Check_In_Quantity,
                ld.Check_In_Price, ld.Purchase_Order_Quantity, ld.Purchase_Order_Price)
 
    LotDetailsDataGrid.DataSource = query.ToList
End Sub

Since I removed the Using statements, should I be concerned about connection issues?
Any thoughts.

Thanks
Bob
0
Julian Benkov
Telerik team
answered on 08 May 2012, 02:54 PM
Hello Bob,

Thank you for writing.

In the case, when you are using lazy loading, you should avoid using 'Using statements'. In this scenario, RadGridView binds to IEnumerable collection and the LINQ query must enumerate all records before disposing the Connection. More information about this topic you can be found in this Stackoverflow thread.

I hope you find this information useful. Let me know if you need further assistance.

Kind regards,
Julian Benkov
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
0
Artiom
Top achievements
Rank 1
answered on 16 May 2012, 08:03 PM
Hello,

So, as the Stackoverflow answers say, I should manualy call Dispose() . It is clear that I should do that after the grid has enumerated all the records. But when does it happens? As I saw after GridModel constructor runs, the IQeryble is still not enumerated.
0
Julian Benkov
Telerik team
answered on 21 May 2012, 04:06 PM
Hi Artiom,

RadGridView creates internal IList and enumerates all records from the LINQ query to fill the internal IList wrapper. This processing is executed when data binding operation is executed or when you setup the DataSource property of RadGridView.

I hope this information is helpful. Let me know if you need further assistance.

Greetings,
Julian Benkov
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
Tags
GridView
Asked by
Bob
Top achievements
Rank 2
Answers by
Bob
Top achievements
Rank 2
Julian Benkov
Telerik team
Artiom
Top achievements
Rank 1
Share this question
or