Please help me, I am new to EF.Lazy loading for POCO objects doesn’t seem to be working.
- My POCO classes are in a sepearte assembly, other than the one one for Data access(i.e DAL)
-
The Data Acess layer simply wraps the calls made to the EF’s object context. Please see the code below
public FilterMaster GetFilter(long ID) { FilterMaster entity = new FilterMaster(); try { using (var context = new RadarEntities()) { //context.ContextOptions.LazyLoadingEnabled = false; //context.ContextOptions.ProxyCreationEnabled = true; entity = context.FilterMasters.SingleOrDefault(filter => filter.ID == ID); //context.FilterMasters.Include( context.LoadProperty(entity, "SQLQuery"); } }- When DAL call is completed, the ObjectContext is lost, and when I tried to fetch the related child objects of the Root POCO class, I get null.
- I’ve tried explicitly enabling ProxyCreation, EnabledLazyLoading, checked the proxy clases generated are not sealed and all the related properties are marked virtual (as suggested on some other links).
-As the lazy load was not working, I thought of eagerly loading all the related POCO objects, so tried invoking LoadProperty method, which works.
Q1: Am I Imissing something the lazy loading isn’t working?
Q2: If I want to expelictly load all the related child objects the will have to call the LoadProperty method for all properties or there is any simpler way?
You are disposing your
ObjectContext. This is what is preventing you from using LazyLoading. If you need LazyLoading, the class containingGetFiltershould create anObjectContextwhen it is created, implementIDisposable, and dispose of theObjectContextwhen it is disposed.