Reason
The navigation properties of the entity are used in view, but the context of the entity is already disposed of by using the controller, but the entity has deferred Query evaluation nature, so The navigation Property object was not loaded, resulting in the above error.
Solution Solution One:How to:explicitly Load related Objects
Disable the deferred query Evaluation for the context and then manually load all the required navigation properties after the query finishes
Falseif (! Contact. salesorderheaders.isloaded) {Contact . Salesorderheaders.load ();}
Workaround Two:How to:use Query Paths to Shape Results
var contacts = ( from in context. Contacts . Include ("salesorderheaders.salesorderdetails") Select Contact). FirstOrDefault ();
Workaround Three: Become list<t>
Summary:
In LINQ to entity, the ObjectContext instance is freed and cannot be queried through the navigation properties. The essence of the navigation property is to resend a command to find the foreign key table for the database execution, the object instance of the connection database is freed, and the query operation is naturally impossible. To avoid such errors, try to convert the data to the List<t> data collection first.
The Entity Framework appears "This ObjectContext instance has been freed and cannot be used for operations that require a connection" error