6
votes

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

This seems to work well but when I set the default constructor to private (to force my code to use the custom version) and request an entity from the database, I don't seem to be able to navigate over related entities as they are all null.

This seems to be a lazy loading problem so I could change my repository to eager load the related objects I need but am wondering if there is a better way to hide the default constructor from the client code whilst allowing Entity Framework to lazy load?

1

1 Answers

12
votes

If you define private constructor you violate requirements for creating POCO proxy responsible for lazy loading:

A custom data class must have a public or protected constructor that does not have parameters.

So the best option for you is using protected constructor or not using lazy loading.