This is a migrated thread and some comments may be shown as answers.

DuplicateKeyException trying to Remove and Add

5 Answers 128 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Gene Allen
Top achievements
Rank 1
Gene Allen asked on 06 Apr 2010, 10:24 PM

Hello, I'm new to Telerik and I'm trying having trouble getting a remove and readd to work.

Here is my test code snippet:

      InspectionReport testNewIR = CopyIR(iScope, ir);  
      string IR_ID = irID.ToString();  
      InspectionReport air = InspectionReport.LoadIR(IR_ID, iScope);  
 
      iScope.Transaction.Begin();  
      iScope.Remove(air);  
      iScope.Transaction.Commit();  
 
      iScope.Transaction.Begin();  
      iScope.Add(testNewIR);  
      iScope.Transaction.Commit();  
 

Here is the exception that I'm getting:
 "Instance of 'ppiatm.QAReporter.Data.ActionItem' with identity '714015106-3c49cf84-361b-4805-a8aa-946f0fc04c0d' already exists in the cache of the object scope."

The ActionItem was actually removed from the database, but for some reason it's still in the IScope cache.

What I'm trying to do is update the local database with an updated object from a central server.

Can anyone point me to what I'm doing wrong?

thanks,

Gene

5 Answers, 1 is accepted

Sort by
0
Gene Allen
Top achievements
Rank 1
answered on 07 Apr 2010, 03:11 PM

I couldn't get the dispose to empty the cache either.

This throws the same exception

      InspectionReport testNewIR = MakeNewIR(iScope, ir);  
      string IR_ID = irID.ToString();  
      InspectionReport air = InspectionReport.LoadIR(IR_ID, iScope);  
 
      iScope.Transaction.Begin();  
      iScope.Remove(air);  
      iScope.Transaction.Commit();  
      iScope.Dispose();  
 
      IObjectScope kScope = ObjectScopeProvider2.GetNewObjectScope();  
      kScope.Transaction.Begin();  
      kScope.Add(testNewIR);  
      kScope.Transaction.Commit();  
 

Any help would be great!

Thanks
0
Gene Allen
Top achievements
Rank 1
answered on 08 Apr 2010, 06:29 PM
I was able to figure out my problem.  Please consider the matter closed. :)
0
Aaron Drenberg
Top achievements
Rank 1
answered on 05 May 2010, 09:16 AM

Hi Gene,

I am having the same problem, and it occurs even when I try to execute a transaction against a new object scope. Would you be able to post your solution?

Thanks.
0
Gene Allen
Top achievements
Rank 1
answered on 05 May 2010, 02:27 PM
For me , the problem was a reference was missing. 
A child object wasn't pointing to it's parent, as soon as I fixed all the various object reference links, the error went away.

Hope it helps,

Gene
0
Aaron Drenberg
Top achievements
Rank 1
answered on 05 May 2010, 04:53 PM

Gene,

Thanks for your reply; that does help me understand a little more about when/why this might occur.

I found the source of my own error last night. One of the objects on my graph had been retrieved from the View State. Unfortunately, the persistent entities cannot be serialized directly and then reattached. The recommended solution in my case is to use the Object Container.

Aaron
Tags
Development (API, general questions)
Asked by
Gene Allen
Top achievements
Rank 1
Answers by
Gene Allen
Top achievements
Rank 1
Aaron Drenberg
Top achievements
Rank 1
Share this question
or