简体   繁体   English

通过服务在数据库中插入实体时出现问题

[英]Problem when inserting an entity in the database through a service

I have a Silverlight application and I'm using a WCF service to access my database information. 我有一个Silverlight应用程序,正在使用WCF服务访问我的数据库信息。 For this I'm using EntityFramework. 为此,我正在使用EntityFramework。 I have a class Items (mapped on my DB table Items) which has an ObservableCollection of Keywords (class mapped on my DB table Keywords) objects. 我有一个Items类(映射到我的数据库表Items),该类具有一个ObservableCollection关键字(映射到我的DB表Keywords上的类)对象。

From the interface I create an Items object with all the properties set including the Keywords list (if the keyword already exists I add the object retrieved by EntityFramework and if a keyword doesn't exist I create from the interface a new Keywords object and add it to my ObservableCollection of my Item). 从界面中,我创建一个具有所有属性集(包括关键字列表)的Items对象(如果关键字已经存在,则添加由EntityFramework检索到的对象;如果不存在关键字,则从界面中创建一个新的关键字对象,并将其添加到我的商品的ObservableCollection中)。 When I pass the Item object to the service and try to add it to the DB, I get the following error: An object with the same key already exists in the ObjectStateManager. 当我将Item对象传递给服务并尝试将其添加到数据库时,出现以下错误: ObjectStateManager中已经存在具有相同键的对象。 The ObjectStateManager cannot track multiple objects with the same key. ObjectStateManager无法使用相同的键跟踪多个对象。

How can I solve this problem? 我怎么解决这个问题?

Looks like the context already contains your object. 看起来上下文已经包含您的对象。 ... check if Attach works instead of add... ...检查附件是否起作用,而不是添加...

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

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