0
votes

I think I must be missing something really obvious here. I'm using visual studio 2012, with .Net 4.5 as the project framework. I'm using datbase first to generate a model (update model from database, select the tables) from my database. I've tried using the default entity generator and by explicitly select EF5.x DBContext generator, and I've made sure that lazyloading is enabled on the model properties at design time and in the context configuration at run time. Despite all that, my properties refuse to lazy load.

I set up a basic example. Table A has a one to many relationship with table B. The code generated by the model includes an overrridable/virtual ICollection property in entity A for this relationship. I then do a Find() to load a single entity A, and access the relationship (eg, objA.EntityBList.Count()), but the collection is always empty. If I load entityB its entityA property is always Nothing.

Should this just work out of the box like this? Previously I've mostly use ObjectContext, so this is a bit new to me.


On further testing, it seems that my simple example does actually work, so the problem seems to be with a specific edmx file. So the question becomes why would an edmx file not produce a model that support lazy loading? I can see that the various materialisation methods are producing POCO objects, rather than proxies, whihc explains why the lazy loading isn't working, but why would I not get proxies. The configuration indicates that proxies are enabled.

1

1 Answers

0
votes

After a fair bit of poking around, including recreating the model from scratch, which helped in a new solution, but not in the original one, I found the solution. In the end there were about 12 entities in the model that weren't being created as proxies, the rest were fine. It seems that each of these, in a hitherto forgetten partial class, had a "Friend Sub New" defined. Removed this and all was well. So I guess the lesson to be learned is to get rid of any extra baggage attached to your classes as the first step.