我错过了什么? (此 EntitySet 中已存在具有相同标识的实体。)
我在实体框架场景中使用 DTO 和转换器。我想更新一个实体。所以我发表这样的声明: databasecontext.Tablename(s).Attach(entityobj);
每当它到达这里时,程序都会以以下错误结束: 此 EntitySet 中已存在具有相同身份的实体
。
我觉得我可以通过说 databasecontext.Tablename(s).Detach(entityobj);
来解决这个问题 在我再次打电话给Attach之前。但程序在 Attach 语句上仍然失败。
我缺少什么?数据库上下文中的哪个方法调用域服务中的更新?
I am using DTOs and converters in an entity framwork scenario. I want to update an entity. So I put this statement:databasecontext.Tablename(s).Attach(entityobj);
Whenever it gets here the program ends with this error:An entity with the same identity already exists in this EntitySet
.
I felt I could get around this by saying databasecontext.Tablename(s).Detach(entityobj);
before I called Attach again. But the program still fails on the Attach statement.
What am I missing? Which method on the DB context calls Update in the Domain Service?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题不清楚。您没有提到数据库上下文的存在时间以及何时将实体加载到上下文中。
但根据你的问题,我猜你已经加载实体将其作为 DTO 传递到某处,并且当返回 DTO 时,你仍然使用相同的上下文。您将 DTO 转换为实体并尝试再次附加 =>砰的异常。同一实体不能两次放入上下文中。尝试改用 ObjectContext.ApplyCurrentValues (EF 4.0) 或 ApplyPropertyChanges (EF 1.0)。
The question is not clear. You didn't mentioned how long does your database context live and when did you load entity into context.
But based on your question I guess that you have loaded entity passed it somewhere as DTO and when the DTO is returned you are using still the same context. You convert DTO to entity and you are trying it to attach again => BANG exception. The same entity cannot be placed twice into context. Try to use ObjectContext.ApplyCurrentValues instead (EF 4.0) or ApplyPropertyChanges (EF 1.0).
看起来您在进行更改时保持上下文打开。
在这种情况下,您不需要将对象重新附加到上下文。
如果打开上下文,从上下文获取对象,然后关闭上下文,对对象进行一些更改,然后打开新上下文。在这种情况下,您需要将对象附加到上下文。
It looks like you are keeping the context open while you make the changes.
In that case you do not need to reattach the object to the context.
If you open a context, get an object from a context, then close the context, make some changes to the object, then open a new context. In this case you need to attach the object to the context.