转换POCO对象中的EntityFramework代理对象 [英] Convert POCO object to Proxy object in EntityFramework

查看:246
本文介绍了转换POCO对象中的EntityFramework代理对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVC3项目,我遇到了一个问题。我有一个创建控制器,它作为一个参数我POCO对象之一。我将这个对象添加到数据库中是这样的:

I have a MVC3 Project and I have run into a problem. I have a Create controller which takes as a parameter one of my POCO objects. I add this object to the database like this:

  entity = dbSet.Add(entity);

此方法返回后,我想用对象的延迟加载功能。不幸的是,对象不是由产生的EntityFramework代理对象...有没有办法以某种方式解决这个问题?

After this method returns, I would like to use the lazy loading features of the object. Unfortunately the object is not a Proxy object generated by the EntityFramework...Is there a way to somehow solve this?

感谢您,
AFrieze

Thank You, AFrieze

推荐答案

该实体必须在它传递给添加方法已经代理。 添加方法将不会返回另一个类的实例,你不能改变现有的实例来代理类型的类型。

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.

您的选项有:


  • 不使用实体作为控制器的输入 - 使用一些视图模型并填充由 dbSet.Create 创建新的实体 - 这将创建空的代理独立实体

  • ,而不是添加实体的控制器接收通过创建 dbSet.Create 一个新的,从接收到的一个数据复制到创建了一个

  • 请不要使用默认的模型粘合剂。创建自定义模型粘合剂(也就是code负责从HTTP请求中提取数据,并填充传递给控制器​​参数)将使用 dbSet.Create 而不是默认的实体构造

  • 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.

这篇关于转换POCO对象中的EntityFramework代理对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆