Issues with Lazy Loading

Jan 7, 2011 at 4:51 PM

I'm having some issues with the Lazy loading behavior of the generated classes. To demonstrate I built a database which captured a many to many relationship with between foos and bars.  

The DB has  foos: (fooId int, fooName varchar(50)) bars: (barId int, barName varchar(50)) and foobars (fooId int, barId int) as a PK and put some test data.

My simple as possible test code is

         using (foobarEntities context = new foobarEntities())            {            
 var firstFoo = context.foos.FirstOrDefault();                                
int barcount = firstFoo.bars.Count();                                
ViewData["Message"] = String.Format("There are {0} bars", barcount);

This works as expected (my first foo has three bars) with the default EF behavior, it continues to work if a make CodeGeneration Strategy None and add in the ADO.NET POCO Entity Generator but  barcount becomes zero (always) if I instead drop the EF4Templates in.  I thought the EF4Template code generation was based on the ADO.NET POCO Entity Generator? Is there something I can do to reinstate the behavior or even to force the loading manually in code ?

Thank in advance.

(What I'm really trying to do would be the equivalent of iterating all of the bars for certain selected foos, but the above case appears to demonstrate the problem sufficiently)

Jan 10, 2011 at 6:46 PM

OK - well I can force the load with

context.LoadProperty(firstFoo, f=>f.bars); 

which is progress of a sort, and from examining the generated model, I see that the properties are not virtual, nor are they ICollections, which explains why it does not work without the explicit load, however, I must have misunderstood the compatibility with the POCO Entity Generator derived classes : or am I still missing something?