Private POCO object constructor preventing lazy loading

I have a POCO object on which I defined a custom constructor. I also implemented a default constructor so that the Entity Framework can successfully clean up the object when I request a copy from the database.

This works well, but when I set the default constructor to private (to force my code to use the user version) and query the entity from the database, I seem to be unable to navigate through related objects as they are all zero.

This seems to be a lazy loading issue, so I can change my repository to load the related objects I need, but I wonder if there is a better way to hide the default constructor from client code, allowing the Entity Framework to lazy load

+5
source share
1 answer

If you define a private constructor, you violate the requirements for creating a POCO proxy server responsible for lazy loading:

A custom data class must have an open or protected constructor that has no parameters.

Thus, the best option for you is to use a protected constructor or not to use lazy loading.

+10
source

All Articles