实体框架ctp5获取未代理的实体 [英] entity framework ctp5 get unproxied entity

查看:98
本文介绍了实体框架ctp5获取未代理的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

EF CTP5.我有一个实例,希望获得未代理的实体.我似乎找不到解决办法.我不想一起全部禁用代理创建,只需要对这一查询使用它.有人可以帮忙吗?

EF CTP 5. I have a single instance where I would like to get the unproxied entity. I can't seem to find a way to do this. I don't want to disable proxy creation all together, just need it for this one query. Can anyone help?

这是一个简单的例子:

var myEntity = DbContext.Entities.Find(1);
var unproxy = myEntity...?

推荐答案

我相信唯一的可能性是创建DbContext的新实例并关闭代理创建以仅执行此查询.原因是DynamicProxy是在运行时创建的类型,它是从您的原始实体类型派生的,并添加了跟踪和延迟加载功能.用这种方式创建代理后,您将无法删除它.试试这个:

I believe the only possibility is to create new instance of DbContext and turn proxy creation off just to execute this query. The reason is that DynamicProxy is type created in runtime which derives from your original entity type and adds tracking and lazy loading functionality. You can't strip the proxy away once you created it this way. Try this:

using (var context = new MyDbContext(connectionString))
{
  ((IObjectContextAdapter)context).ObjectContext.ContextOptions.ProxyCreationEnabled = false;
  var myEntity = context.Entities.Find(1);
}

这篇关于实体框架ctp5获取未代理的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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