简体   繁体   中英

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. For this I'm using 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.

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). 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. The ObjectStateManager cannot track multiple objects with the same key.

How can I solve this problem?

Looks like the context already contains your object. ... check if Attach works instead of add...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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