Reputation: 29032
I have an object defined by my entity framework model that has navigation properties, but whenever the navigation property is null, entity framework seems to try to pull it down again from the database. This is fine for now and I can work on this problem later, but when it tries to get the navigation property from the database, I get the error from the model:
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
Well this isn't too good that it's blowing chunks like this. Is there anything I can do to ensure that this is only attempted when the context is still open?
Thanks in advance!
Upvotes: 0
Views: 440
Reputation: 9431
EF framework also supports 'eager loading'. This means that the related entities will be returned in the same query. For this of course you have to know in advance which related entities you need, but this is a technique I use a lot. It might be worth giving it a shot in your scenario.
Here is same sample code of MSDN: (http://msdn.microsoft.com/en-us/library/bb896272.aspx)
// Define a LINQ query with a path that returns
// orders and items for a contact.
var contacts = (from contact in context.Contacts
.Include("SalesOrderHeaders.SalesOrderDetails")
select contact).FirstOrDefault();
Upvotes: 1
Reputation: 126567
You have lazy loading turned on, but you've disposed your ObjectContext
. Either don't dispose it so soon or turn off lazy loading.
Upvotes: 2