银光科技RIA& POCO:插入两个新的子实体时,SubmitOperationFailed.存在具有相同ID的实体 [英] Silverlight & RIA & POCO: SubmitOperationFailed when INSERTING two new child entities. An entity with the same ID exists

查看:147
本文介绍了银光科技RIA& POCO:插入两个新的子实体时,SubmitOperationFailed.存在具有相同ID的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的情况:

我正在使用Silverlight,RIA和POCO对象(没有Entity Framework;我们正在针对Oracle和SP进行工作).

I am using Silverlight, RIA and POCO objects (no Entity Framework; we're working against Oracle and SP's).

我有一个Parent对象,其中包含Child对象的集合.我已经在父级上正确设置了关联和组成属性.当我要保存更改时,整个对象图将正确发送到服务器.

I have a Parent object that contains a collection of Child objects. I have setup the Association and Composition attributes on the Parent correctly. When I want to save changes, the entire object graph gets sent to the server correctly.

用户可以将一个或多个子对象添加到父对象.

The user can add one or more Child objects to the Parent.

现在,如果用户将一个子对象添加到父对象并保存,则一切正常.但是,当用户尝试将两个或两个以上新对象添加到父对象"然后继续存在时,出现经典错误:

Now, if the user adds ONE Child object to the Parent and saves it then everything works. However, when the user tries to add TWO or more new objects to the Parent and then persist, I get the classic error:

System.ServiceModel.DomainServices.Client.DomainOperationException:提交操作失败.具有相同标识的实体已存在于此EntitySet中. ---> System.InvalidOperationException:具有相同标识的实体已存在于此EntitySet中.

System.ServiceModel.DomainServices.Client.DomainOperationException: Submit operation failed. An entity with the same identity already exists in this EntitySet. ---> System.InvalidOperationException: An entity with the same identity already exists in this EntitySet.

现在,这在客户端上失败了.我正在跟踪所有内容-数据库实际上已更新!一切都正确地发送到服务器,数据库得到更新.我在重新查询发生时检查服务器上的对象键,它们正确无误-所有新的子对象都按顺序将其ID的值从零更新为实数.

Now, this is failing on the client. I am tracing everything - the database actually gets updated! Everything gets sent down to the server correctly, the DB gets updated. I check the object keys on the server when the re-query happens and they are correct - all of the new child objects get their ID's updated from zero to a real number in sequence.

这是我在客户端上重新加载Parent对象时遇到的错误.我不明白我正在重新加载操作上新建一个Context;它应该为空,并且仅加载父级和关联的子级.我先在服务器端检查数据,然后再使用查询方法-父级和子级数据都可以.那是怎么回事?为什么我的上下文对无法完成此SubmitOperation感到不安?

It's when I get to re-load the Parent object on the client that I get this error. I don't get it. I am newing up a new Context on the re-load operation; it should be empty and just load the Parent and associated Children. I check the data on the server side before it goes out of the query method - the parent and child data is fine. So what's happening? Why is my context bitching about not being able to complete this SubmitOperation?

推荐答案

您放弃太容易了-不要让RIA为您做! :-)

You gave up too easy - don't let RIA do it for you!! :-)

这是交易...

由于您正在使用POCO对象(无EF),因此您很可能在其中一个属性上具有标识属性([Key]),以指定该对象的密钥(或身份).

Since you are working with POCO objects (no EF) you most likely have an identifying attribute ([Key]) on one of your properties designating it the key (or identity) of that object.

好吧...

当添加2个连续对象时,很可能会将键的默认值默认为0.这是域服务&的问题.上下文,因为它尝试为您管理集合.好吧,如果将对象保存到数据库之后,您还没有更新密钥,那么它们都将保留为0.

When you add 2 consecutive objects, you will most likely default the value of your key to a value of 0. This is a problem for the domain service & context because it attempts to manage the set for you. Well, if after the saving of the objects to the database, you have not updated the key they will both remain with a value of 0.

问题!

域服务&上下文尝试将这两个对象放入其托管EntitySet中,因此,所有对象都必须是唯一的.

The domain service & context attempt to put these two objects in its managed EntitySet and, as such, all objects must be unique.

所以...

长而短的是...将键值保存到数据库后,对其进行更新.

Long and the short of it is... update your key value after saving it to the database.

这篇关于银光科技RIA& POCO:插入两个新的子实体时,SubmitOperationFailed.存在具有相同ID的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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