使用 ADO.NET 实体框架时出错

发布于 2024-08-24 06:29:37 字数 393 浏览 7 评论 0原文

我想将列表转换为 EntityCollection。

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

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

我收到这个错误。

无法将对象添加到 EntityCollection 或 EntityReference。 附加到某个对象的对象 无法将 ObjectContext 添加到 EntityCollection 或 EntityReference 与来源无关 对象。

有人知道这个错误吗?

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花想c 2024-08-31 06:29:37

听起来 x 是 ObjectContext 查询的结果。每个 ObjectContext 跟踪它从数据库读取的实体以启用更新方案。它跟踪实体以了解它们何时(或是否)被修改,以及哪些属性被修改。

术语是实体附加到ObjectContext。在您的情况下,x 中的实体仍附加到具体化它们的 ObjectContext,因此您无法同时将它们添加到另一个 EntityCollection 中。

如果您首先分离它们,您也许能够做到这一点,但如果您这样做,第一个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.

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.

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.

如日中天 2024-08-31 06:29:37

基本上所有实体对象都由充当更改跟踪器的对象上下文控制。这里的想法是,实体本身对其环境来说是愚蠢的,但对象上下文知道发生了什么。

这是数据集模型的反转,其中表跟踪它们自己的更改。

因此,对象被直接添加到对象上下文及其实体集合中。在这里,您创建了一个不与对象上下文关联的 EntityCollection,因此不能向其中添加其他对象。它们必须首先附加到对象上下文。

实际上,您可能想要的是返回 IQueryable 而不是 IList。这将允许您对 methodcall() 的结果执行查询。

Basically all entity objects are controlled by an object context which serves as change tracker. The idea here is that the entities themselves are dumb to their environment, but the object context knows what's going on.

This is an inversion of the DataSet model where the tables track their own changes.

So objects are added to an object context and its entity collections directly. Here you've created an EntityCollection that's not associated with an object context and therefore can't have other objects added to them. They must first be attached to the object context.

Really what you probably want is to return IQueryable instead of IList. That would allow you to execute queries against the results of methodcall().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文