简体   繁体   English

使用RIA Services更新实体时出现奇怪的InvalidOperationException

[英]Strange InvalidOperationException when updating entity with RIA Services

I'm working with EF4.1, RIA Services and Silverlight. 我正在使用EF4.1,RIA服务和Silverlight。 I'm having a somewhat bizarre problem in an update scenario. 我在更新方案中遇到了一个奇怪的问题。

The domain model is quite simple; 领域模型非常简单; it deals with Requests and Persons . 它处理请求 They have a 1-to-n relationship. 他们有一对一的关系。 So a Citizen can have multiple Requests although in reality this will never occur since the app simply does not provide functionality to do so. 因此,一个公民可以有多个请求,尽管实际上这永远不会发生,因为该应用程序根本不提供这样做的功能。

Request has a property called 'Urgent', which I change to true and then try to save. 请求具有一个名为“紧急”的属性,我将其更改为true ,然后尝试保存。 All goes well until the actual persisting begins via this method: 一切顺利,直到通过此方法开始实际持久化为止:

    public void UpdateRequest(Request currentRequest)
    {
        Request original = ChangeSet.GetOriginal(currentRequest);
        try
        {
            ObjectContext.Requests.AttachAsModified(currentRequest, original);
        }
        catch (Exception ex)
        {
            // weirdness here!
        }
    }

which is pretty much the standard generated method by RIA Services (except the try/catch handler which I added for debugging purposes.) I then get the following error: 这几乎是RIA Services生成的标准方法(除了为调试目的而添加的try / catch处理程序。)然后,我得到以下错误:

When I check the ChangeSet, there are no Requests to be added, so I'm sure I didn't add it by accident. 当我检查ChangeSet时, 没有要添加的请求,因此我确定我不是偶然添加的。

An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.

i don't understand this... There are literally no added objects in the ObjectStateManager, the ChangeSet has no added objects; 我不明白这一点... ObjectStateManager中实际上没有添加对象,ChangeSet也没有添加对象; where the hell is this coming from? 这到底是哪里来的? I tracked which properties are being changed, so I'm sure the key is not overwritten, nor is it being added or some other funkyness. 我跟踪了正在更改的属性,所以我确定密钥不会被覆盖,也不会被添加或出现其他一些时髦。

Can anyone shed some light here? 谁能在这里阐明一些想法? Driving me crazy for several days so far... 到目前为止让我疯狂了好几天...

I managed to fix it using the following logic, basically we're checking if the entity is attached already. 我设法使用以下逻辑对其进行了修复,基本上我们正在检查该实体是否已附加。 If it is, we don't re-attach it but just update the values. 如果是这样,我们不会重新附加它,而只是更新值。 Otherwise, we attach it. 否则,我们将其附加。

        ObjectStateEntry entry;
        // Track whether we need to perform an attach
        bool attach;
        if (ObjectContext.ObjectStateManager.TryGetObjectStateEntry(ObjectContext.CreateEntityKey("Requests", currentRequest), out entry))
        {
            // Re-attach if necessary
            attach = entry.State == EntityState.Detached;
        }
        else
        {
            // Attach for the first time
            attach = true;
        }
        if (attach)
        {
            ObjectContext.DocumentRequests.AttachAsModified(currentRequest, original);
        }
        else
        {
            ObjectContext.Requests.ApplyCurrentValues(currentRequest);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM