I am using Linq to SQL classes for the data extraction and have the below entities:
- Customer -> Orders
Customer and Order are linked via a FK constraint on the CustomerID and appear in my .dbml file with that relationship
When I access the object properties via intellisense I can see Customer.Name, Customer.Address etc but also the linked Orders entity Customer.Orders
My issue is that when I query the database to populate the Customer entity only and dispose the Datacontext I get an error thrown
Cannot access a disposed object. Object name: 'DataContext accessed after Dispose.'.
public IEnumerable<Customer> GetCustomer(Int32 customerID)
{
// initialise
Customer cust = new Customer();
using (CustomerManager ctl = new CustomerManager())
{
// get customer, this returns a single customer entity
// i.e. datacontext.Customers.Where(m=> m.CustomerID == customerID).FirstOrDefault();
cust = ctl.SelectCustomer(customerID);
}
yield return cust;
}
I have tried changing this to a Stored procedure i.e. Customer_SelectResult and this works perfectly fine. It is when I use the tables.
Is there any way to prevent the child tables from causing this issue?
Thanks in advance