即使我相信它没有附加,EF4也不能向上下文添加一个对象 [英] EF4 can’t add an object to the context even though I’m convinced it is not attached

查看:108
本文介绍了即使我相信它没有附加,EF4也不能向上下文添加一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能向ObjectContext添加一个对象,即使我确信它没有被附加,并且它不与另一个上下文相关联。

I can’t add an object to the ObjectContext even though I’m convinced it is not attached and it is not associated with a different context

我正在使用EF 4与POCO对象。

I am using EF 4 with POCO objects.

如果我尝试

_currentContext.ObjectStateManager.GetObjectStateEntry(entityIn)

然后我收到错误

The ObjectStateManager does not contain an ObjectStateEntry with a reference to an object of type 'System.Data.Entity.DynamicProxies.OrderItem_7D361CB49D75AA90681B4BA3F924139ECB0FC1426E38E90C7B884A4E9CD777DF'.

公平的不在那里。所以如果我尝试附加它

Fair enough it’s not there. So if I try to attach it

_currentContext.AttachTo("OrderItems", entityIn)

然后我得到

An entity object cannot be referenced by multiple instances of IEntityChangeTracker.

我如何获得这两个错误。他们不矛盾吗?如何将其附加到上下文?

How can I be getting both errors. Don’t they contradict?? How can I attach it to the context?

修改

整个应用程序的一个上下文该模式是

There is only one context throughout the app. The pattern is


  1. 通过存储库对象的所有调用。

  2. 对象从工厂获取其对象文本类。

  3. 此工厂类可以创建上下文或从请求对象获取上下文,从而保证页面生命周期中只使用一个上下文

在任何时候,如果你采用两个上下文对象的实例,每个哈希码都是一样的。

At any point if you take two instantiations of the context object the hash code for each one is the same.

推荐答案

对象 entityIn 可能引用了另一个上下文中的对象。

The object, entityIn probably has a reference to an object which is in another context.

然而,这里的真正的问题是你的范围中有多个上下文 在Web应用程序中,每个请求应该有一个上下文。

However, the real problem here is that you have multiple contexts in scope at all. It is hard to do that right. In a web application, you should have one context per request, period.

这篇关于即使我相信它没有附加,EF4也不能向上下文添加一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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