实体框架代码优先 - DbContext 上没有 Detach() 方法 [英] Entity Framework Code First - No Detach() method on DbContext

查看:21
本文介绍了实体框架代码优先 - DbContext 上没有 Detach() 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么 DbContext 对象上没有像 ObjectContext 那样的 Detach 方法.我只能假设这个遗漏是故意的,但我很难弄清楚为什么.我需要能够分离和重新附加实体(例如,用于将缓存放入 ASP.NET 项目中).但是,由于我无法分离实体,因此当我尝试附加与先前上下文关联的实体时,出现实体对象不能被 IEntityChangeTracker 的多个实例引用"异常.

I'm wondering why there is no Detach method on the DbContext object like there is for ObjectContext.  I can only assume this omission was intentional, but I have a hard time figuring out why.  I need to be able to detach and re-attach entities (for putting in the cache in an ASP.NET project, for example).  However, since I can't detach an entity, when I try to attach an entity that was associated with a previous context, I get the "An entity object cannot be referenced by multiple instances of IEntityChangeTracker" exception.

这里的指导是什么?我错过了什么吗?

What's the guidance here?  Am I missing something?

推荐答案

对于可能会遇到这个问题的人,从 CTP5 开始,您现在需要编写

For people that might stumble upon this question, as of CTP5 you now need to write

((IObjectContextAdapter)context).ObjectContext

为了获得 ObjectContext.

in order to get to ObjectContext.

这篇关于实体框架代码优先 - DbContext 上没有 Detach() 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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