简体   繁体   English

Silverlight&RIA&POCO:插入两个新的子实体时,SubmitOperationFailed。 存在具有相同ID的实体

[英]Silverlight & RIA & POCO: SubmitOperationFailed when INSERTING two new child entities. An entity with the same ID exists

Here's my scenario: 这是我的情况:

I am using Silverlight, RIA and POCO objects (no Entity Framework; we're working against Oracle and SP's). 我正在使用Silverlight,RIA和POCO对象(没有实体框架;我们正在针对Oracle和SP进行工作)。

I have a Parent object that contains a collection of Child objects. 我有一个Parent对象,其中包含Child对象的集合。 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: Submit operation failed. System.ServiceModel.DomainServices.Client.DomainOperationException:提交操作失败。 An entity with the same identity already exists in this EntitySet. 具有相同标识的实体已存在于此EntitySet中。 ---> System.InvalidOperationException: An entity with the same identity already exists in this EntitySet. ---> System.InvalidOperationException:具有相同标识的实体已存在于此EntitySet中。

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. 我在重新查询发生时检查服务器上的对象键,它们正确无误-所有新的子对象都按顺序将其ID的值从零更新为实数。

It's when I get to re-load the Parent object on the client that I get this error. 这是我在客户端上重新加载Parent对象时遇到的错误。 I don't get it. 我不明白 I am newing up a new Context on the re-load operation; 我正在重新加载操作上新建一个Context; 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? 为什么我的上下文对无法完成此SubmitOperation感到困惑?

You gave up too easy - don't let RIA do it for you!! 您放弃太容易了-不要让RIA为您做!! :-) :-)

Here is the deal... 这是交易...

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. 由于您正在使用POCO对象(无EF),因此您很可能在其属性之一上具有标识属性([Key]),以指定该对象的键(或标识)。

Well... 好...

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. 当您添加2个连续对象时,您很可能会将密钥的默认值默认为0。这对于域服务和上下文是一个问题,因为它会尝试为您管理集合。 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. 好吧,如果在将对象保存到数据库之后,您尚未更新密钥,则它们都将保留为0。

Problem! 问题!

The domain service & context attempt to put these two objects in its managed EntitySet and, as such, all objects must be unique. 域服务和上下文尝试将这两个对象放入其托管EntitySet中,因此,所有对象都必须是唯一的。

So... 所以...

Long and the short of it is... update your key value after saving it to the database. 多头和空头是...将键值保存到数据库后更新。

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

相关问题 RIA服务返回Silverlight POCO - Silverlight POCO returned by RIA services 具有相同标识的实体已存在于此EntitySet中。 -WCF RIA服务 - An entity with the same identity already exists in this EntitySet. - WCF RIA Services 插入与现有实体有关系的新实体时出现的问题(Entity Framework Core 1.1.0) - Issue when inserting new entity with relationships to existing entities (Entity Framework Core 1.1.0 ) 实体框架:将现有子POCO添加到新的父POCO,在DB中创建新子项 - Entity Framework: adding existing child POCO to new Parent POCO, creates new child in DB 检测何时在实体框架中初始化POCO实体 - Detecting when POCO entities are being initialized in Entity Framework 实体框架:添加到新实体时,如何使用来自实体B的自动生成ID作为实体A中的外键? - Entity framework: When adding to new entities, how to use autogen ID from entity B as foreign key in entity A? 将子实体移动到新的父实体 - Move child entities to a new parent entity RIA服务:实体框架参考实体 - RIA Services: Entity Framework Reference Entities LINQ to Entities返回实体的孩子的孩子的错误ID - LINQ to Entities returns wrong ID for an entity's child's child 将相同的断开实体的不同实例插入实体框架 - Inserting different instances of same Disconnected Entities into Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM