错误使用ADO.NET实体框架 [英] Error Using ADO.NET Entity Framework

查看:112
本文介绍了错误使用ADO.NET实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个列表转换为EntityCollection。

I want to convert a list to EntityCollection.

List<T> x = methodcall();
EntityCOllection<T> y = new EntityCollection<T>();

foreach(T t in x)
  y.Add(t);

我得到这个错误。

I get this error.

的对象不能被添加到   EntityCollection或的EntityReference。   附加到一个物体   ObjectContext的不能被添加到   EntityCollection或的EntityReference   不与源相关联   对象。

The object could not be added to the EntityCollection or EntityReference. An object that is attached to an ObjectContext cannot be added to an EntityCollection or EntityReference that is not associated with a source object.

任何人都知道这个错误?

Anyone know about this error?

推荐答案

这听起来像 X 是一个ObjectContext的查询的结果。每个ObjectContext的跟踪它从数据库中读取,使更新方案的实体。它跟踪实体知道何时(或如果)被修改,并且其属性被修改

It sounds like x is the result of an ObjectContext query. Each ObjectContext tracks the entities it reads from the database to enable update scenarios. It tracks the entities to know when (or if) they are modified, and which properties are modified.

的术语,该实体的连接应用于ObjectContext的。在你的情况下,在实体X 仍然会附加到ObjectContext的物化,所以你不能将它们添加到另一个EntityCollection在同一时间。

The terminology is that the entities are attached to the ObjectContext. In your case, the entities in x are still attached to the ObjectContext that materialized them, so you can't add them to another EntityCollection at the same time.

您可能能够做到这一点,如果你第一个分离它们,但如果你这样做,第一ObjectContext的停止跟踪他们。如果你不想再更新这些项目,这不是一个问题,但如果你以后需要更新它们,你将不得不连接再次它们。

You may be able to do that if you first Detach them, but if you do that, the first ObjectContext stops tracking them. If you never want to update those items again, it's not a problem, but if you later need to update them, you will have to Attach them again.

这篇关于错误使用ADO.NET实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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