Private constructor on POCO entity preventing lazy loading

和自甴很熟 提交于 2019-12-01 16:36:02

问题


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:


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.



来源:https://stackoverflow.com/questions/9907731/private-constructor-on-poco-entity-preventing-lazy-loading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!