Convert POCO object to Proxy object in EntityFramework

蓝咒 提交于 2019-12-05 15:05:55

The entity must be already proxied when you pass it to Add method. Add method will not return another instance of the class and you cannot change the type of the existing instance to the proxy type.

Your options are:

  • Not using entity as input in controller - use some view model and populate new entity created by dbSet.Create - this will create empty proxied detached entity.
  • Instead of adding the entity received in controller create a new one by dbSet.Create and copy data from received one to created one
  • Don't use default model binder. Create custom model binder (that is the code responsible for extracting data from HTTP request and populating parameters passed to controller) which will use dbSet.Create instead of default entity constructor.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!