Reputation: 22760
I have this code in my dal;
public List<BDO_Enquiry> GetEnquiryList()
{
List<BDO_Enquiry> retList = new List<BDO_Enquiry>();
try
{
using (var context = new BDODevelopmentEntities())
{
try
{
retList = context.BDO_Enquiry.ToList();
}
catch (Exception ex) { string g = ""; }
}
} catch(Exception ex) { string h = ""; }
return retList;
}
However, when i return to my business logic, all children of the ret list are not loaded.
So I have an employee object and in that there is an employer object.
The employee objects are returned but none of the employer child objects are. When I try to access them, I of course get the error System.ObjectDisposedException
How can I do a query and get back all the children as well?
Upvotes: 0
Views: 58
Reputation: 6864
Try...
public List<BDO_Enquiry> GetEnquiryList()
{
List<BDO_Enquiry> retList = new List<BDO_Enquiry>();
try
{
using (var context = new BDODevelopmentEntities())
{
try
{
var query = context.BDO_Enquiry.Include("Child");
query = query.Include("AnotherChild");
retList = query.ToList();
}
catch (Exception ex) { string g = ""; }
}
} catch(Exception ex) { string h = ""; }
return retList;
}
This will load Child and AnotherChild as a part of the one query. If you had attempted to access the navigation properties of the entity within your using statement you would see that SQL requests to load the navigation properties are executed to load the navigation properties on demand. Once you have exited the using statement the context will close and navigation properties are no longer loaded on demand.
Upvotes: 1