实体框架代码首先 - DbContext上没有Detach()方法 [英] Entity Framework Code First - No Detach() method on DbContext
问题描述
我想知道为什么DbContext对象上没有Detach方法,就像ObjectContext一样。我只能假设这个遗漏是有意的,但我很难弄清楚为什么。我需要能够分离和重新附加实体(例如,将缓存放入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屋!