麻烦更新自跟踪实体 [英] Trouble updating self-tracking entities

查看:151
本文介绍了麻烦更新自跟踪实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用自跟踪实体WCF客户端-server应用。我的WCF服务返回的各种实体,而这些可以通过相应的更新方法进行更新。

I'm using self-tracking entities in a WCF client-server application. My WCF service returns various entities, and these can be updated using corresponding Update methods.

这工作得很好了一段时间,但现在我有问题。要保持专注,I'l限制这次讨论到一个特定的情况下,简化为最基本的要素。

This worked well for a while, but now I'm having problems. To keep the focus, I'l limit this discussion to a specific case, simplified to the bare essentials.

我的一个表被称为SystemDefinition。它没有外键,而另一个表(路线)有一个外键给它。因此,它具有在所述实体模型(称为路由)的单个导航属性​​。其他所有列是标量。表,以及相应的实体,有一个名为ID的主键列类型GUID的。该数据库的SQL Server Compact 3.5版。

One of my tables is called SystemDefinition. It has no foreign keys, but one other table (Route) has a foreign key to it. Consequently, it has a single navigation property in the entity model (called Routes). All the other columns are scalars. The table, and the corresponding entities, has a primary key column called Id, of type Guid. The database is SQL Server Compact v3.5.

要重现该问题,我可以:

To reproduce the problem, I can:

  1. 在用我的WCF服务的GetSystem()方法
  2. 检索单个SystemDefinition实体
  3. 在客户端,调用MarkAsDeleted()上的实体
  4. 呼叫UpdateSystem(),通过实体作为参数
  1. Retrieve a single SystemDefinition entity using my WCF service's GetSystem() method
  2. In the client, call MarkAsDeleted() on the entity
  3. Call UpdateSystem(), passing the entity as the parameter

在code在UpdateSystem()仅仅是(删除为清楚起见非必需code):

The code in UpdateSystem() is simply (non-essential code deleted for clarity):

_objectContext.SystemDefinitions.ApplyChanges(system);
_objectContext.SaveChanges();

实体检索,没有包括()子句,这意味着路由集合为空(反正,如果在路由没有行与外键的SystemDefinition仍然出现错误)。所以,我传递给更新方法的SystemDefinition实体是在仅在图实体。然而,我仍然得到以下异常:

The entity is retrieved with no Include() clause, which means the Routes collection is empty (and anyway, the error still occurs if there are no rows in Route with foreign keys to the SystemDefinition). So the SystemDefinition entity I pass to the Update method is the only entity in the graph. Yet I still get the following exception:

InvalidOperationException异常:   的AcceptChanges无法继续,因为   该对象的键值冲突   在另一目的   ObjectStateManager。确保   键值调用之前唯一   AcceptChanges的。

InvalidOperationException: AcceptChanges cannot continue because the object's key values conflict with another object in the ObjectStateManager. Make sure that the key values are unique before calling AcceptChanges.

该异常是由第一个方法调用(ApplyChanges)抛出。我敢肯定的ObjectContext的是新鲜的,每个方法调用创建一个新的。

The exception is thrown by the first method call (ApplyChanges). I am certain the ObjectContext is fresh, a new one created for each method call.

我已经调试了code一路下跌到它抛出(在ObjectContext.FixupKey()),但code变得毫无意义,我,还有微软的源代码$ C无评论$ C,表示这是什么原因造成的条件实际上意味着。

I have debugged the code all the way down to where it throws (in ObjectContext.FixupKey()), but the code makes little sense to me, and there are no comments in Microsoft's source code to indicate what the condition that causes it actually means.

当然,该消息被误导?有参与更新只是一个单一的实体。可能是什么回事?

Certainly, the message is misleading? There's just a single entity involved in the update. What might be going on?

PS。我发现一个论​​坛帖子提示覆盖GetHash code()和equals()的实体类的方法可能会有所帮助。这将使一些意义,如果该错误是由ObjectStateManager不能够确定更新该实体实际上是一样的,在一些情况下引起的实体。我尝试过了(使用部分类),但ufortunately它并没有帮助。所以,现在我迷路了。任何建议将受到欢迎。

PS. I found a forum post suggesting that overriding the GetHashCode() and Equals() methods on the entity classes might help. This would make some sense if the error was caused by the ObjectStateManager not being able to determine that the entity to update is in fact the same as some entity in the context. I tried it out (using partial classes), but ufortunately it didn't help. So now I'm lost. Any suggestions would be welcome.

推荐答案

我怀疑你的 _objectContext 已经包含了您尝试从应用更改实体实例。因为你的实体从外部客户端接收,他们将永远是相同的(在实例职权范围)上下文已经知道这些情况。

I suspect that your _objectContext already contains the entity instances that you try to apply changes from. Since your entities are received from an external client, they will never be the same (in terms of instance reference) to those instances the context already knows about.

此有道理与抛出的异常:上下文结束与含相同键值两个不同的实例

This makes sense with the exception thrown: the context ends up with two different instances containing the same key values.

要解决这一点,你只会确保你总是有新鲜的环境中工作。

To solve this you would simply make sure that you always work with a fresh context.

这篇关于麻烦更新自跟踪实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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