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

DuplicateKeyException when using ObjectContainer

1 Answer 75 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.
James Denning
Top achievements
Rank 1
James Denning asked on 26 Jun 2009, 03:22 PM
HI
Following on from the thread concerning usign one ObjectCOntainer on a disconnected client, I am now getting an error when using the same Container to add a new object then update another where the second update ChangeSet causes a duplicatekeyexception when commiting a Scope transaction as it tries to insert a duplicate of the new object added in the first ChangeSet from the Container. HEre is the sequence of events (the client is via WCF)

1. Get a container from server to client
2. Get reference data from server as a ChangeSet and apply to client container
3. Create a new instance of a persisted object within client container transaction, attach reference objects from container
4. Send changeset to server, apply, copy to scope and commit, receive ChangeSet via  GetContent()  from the server container and apply to client container.
5. Retrieve another preexisting object from server via a GetContent() changeset applied ot client container from a fresh container on the server. Evict the original new object.
6. Edit the preexisting object within client container transaction
7. Send changes to server via GetChanges() the subsequent scope transaction Commit operation (after a CopyTo) on a scope fails with the DuplicateKeyException witht he data indicating it is trying to insert the object form the first phase.

When I use a fresh container for the update phase on the client this error does not occur BUT I have then lost all my cached reference data and would have to refetch it from the server :( . The aim is to always use one container throughout the lifetime of the client application. AM i missing a step here? What ObjectContainer.Verfify modes should I be using in these scenarios?

1 Answer, 1 is accepted

Sort by
0
Alexander
Telerik team
answered on 29 Jun 2009, 01:04 PM
Hello James Denning,

The problem with the duplicating key is because OpenAccess tries to insert the same object twice. The solution is to use the "generateUpdateChanges" option of the ObjectContainer.CommitChanges() method. It can be enabled by passing "True" as last argument to the method and will cause the generation of a ChangeSet with the newly persisted object which you should send back to the client.
I assume you have somewhere a method that applies the changes to an ObjectScope. If necessary, modify it so it should return the ChangeSet from the CommitChanges() method:
[WebMethod] 
public ObjectContainer.ChangeSet StoreAll(ObjectContainer.ChangeSet cs) 
   IObjectScope scope = ObjectScopeProvider1.ObjectScope(); 
 
   return ObjectContainer.CommitChanges(cs, ObjectContainer.Verify.All, scope, truetrue); 

Then, on the client, apply this change set immediately after sending the changes to the server:
ObjectContainer.ChangeSet set = service.StoreAll(container.GetChanges(ObjectContainer.Verify.All)); 
 
container.Apply(set); 

Now the client will treat the the newly inserted object as expected - you can update it and store the changes again.
About the ObjectContainer.Verify modes, All or Changed should work in this case. You can take a look at this help topic for a reference.

Greetings,
Alexander
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Development (API, general questions)
Asked by
James Denning
Top achievements
Rank 1
Answers by
Alexander
Telerik team
Share this question
or