Reputation: 981
I have a 'Layered' Application:
I'm using Entities from EntityFramework model-first approach for all the layers. Also I'm using lazy loading.
For example I have an Employee entity:
[Serializable]
[DataContract]
public class Employee
{
[DataMember]
public string name { get; set; }
[DataMember]
public List<Image> images { get; set; }
}
In the DataLayer I have for example a EmployeeDAO with a operation like:
public List<Employee >GetAll()
{
List<Employee> resultList;
using (ModelContainer ctx = new ModelContainer()) // DbContext
{
resultList = ctx.Employees.All<Employee>().ToList<Employee>();
}
return resultList;
}
The problem is that I'm gettin a exception 'Object Disposed' for the List of Images inside an Employee, and I cant figure out why.
Thanks!!!
Upvotes: 0
Views: 275
Reputation: 364279
Most probably because of lazy loading. Your GetAll
method creates context and disposes the context after loading employees but the lazy loading requires context to stay alive. You must either redesign your application and control context lifetime on the service layer or you must not use lazy loading.
Btw. if service layer represents remote layer (WCF or any other technology) you should not use lazy loading at all.
Upvotes: 1