Sean Gifford
Sean Gifford

Reputation: 33

EF6 only lazy works from loaded context data

Using EF 6 rc 1 Code first Lazy loading is enabled and proxy generation is allowed (defaults).

When attempting to access navigation properties, I am finding that: - if the referenced object was already loaded into the DbContext in some manner, the navigation property will be populated - if the referenced object was not already loaded, the navigation property will remain null. No query attempt is made to the SQL Server DB.

This applies to collections and objects. In all cases, I have verified that the navigation properties are marked virtual.

Is this expected behaviour? I have scroured every article I could find but have found no description of this (please provide links). If not expected behaviour, what could I be doing wrong?

Thank you!

EDIT: Because I can't post the classes I have, I've tried to reproduce this into a simple example I would be comfortable posting. The simple example, however, works as expected. I have noticed one major difference between the working example and the non-working: my non-working application produces entities that are NOT proxies. This, even though proxy generation is allowed, and I am retrieving these objects from a new context (NOT the context they were created in). I can't post the non-working code, and there's no value in posting the working code. Theories explaining why proxy classes are not being generated would be welcome!

Upvotes: 0

Views: 632

Answers (1)

Sean Gifford
Sean Gifford

Reputation: 33

Turned out the reason the Proxy wrapper wasn't being created is because some of the classes had the default constructor marked internal. Simply changing them to protected allowed the Proxy to be created, which then enabled lazy loading. Though I found this to be pretty obscure it is documented here: http://msdn.microsoft.com/en-us/library/vstudio/dd468057(v=vs.100).aspx

Upvotes: 1

Related Questions