0

I have some object with EntityCollection inside. If I just add the new entity everything works good. The code that does it here:

Context.ApplyChanges()
Context.SaveChanges()

But if I try to update the entity I have the following message:

AcceptChanges cannot continue because the object's key values conflict with another object in the ObjectStateManager. Make sure that the key values are unique before calling AcceptChanges.

Even if use the same collection objects. I mean there can not be real the same entity objects in the collection because are saved into database if they are new. But! If I try to apply the same code to the separate entities into collection they are saved but again if I try to apply it on the object (container) I have this error.

Thanks for the help

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
mimic
  • 4,897
  • 7
  • 54
  • 93

2 Answers2

0

Have you tried retrieving the entity you wish to change from the Context, modifying that entity appropriately, then saving changes? If so, could you provide a more detailed example?

Andrew
  • 14,325
  • 4
  • 43
  • 64
  • Yes, I do so. Firstly I just save the entity into the database, then I get it, change (add object into collection) and try to save again and this time I fail. – mimic Mar 23 '11 at 02:16
  • @Seacat What do you mean by "add object into collection?" That may seem obvious, but really, what are you doing with the object? – Andrew Mar 23 '11 at 02:17
  • I have Object with collection, it's TrackableCollection, I just add the new entity and save the container object - everything is saved. But if I then add new object into the collection and try to save, it fails. – mimic Mar 23 '11 at 02:27
  • @Seacat Could you show us some more code, showing the part that works and the part that doesn't? – Andrew Mar 23 '11 at 16:06
0

I suspect you've omitted something from your model or your table schema. This is what I would expect if the model primary key was expected to be autogenerated, but your table/model code doesn't define it that way. You'd then have all the primary keys for the object in the collection set to zero and you get a primary key violation when attempting to insert the second object.

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
  • The keys unfortunately are not autogenerated. But if it's the problem why I can add 2 new entity into a new object and save object and everything is saved but the same time if I have already this object with one of the entity in the collection and then if I just try to add the second (the same as in the first case) I fail? – mimic Mar 23 '11 at 02:24
  • @Seacat - it was just a guess based on what you described. I interpreted what you said to mean that if you added just the entity it worked, but if you added the entity with the associated objects it didn't. That made me think there might be something wrong with the keys in the associated objects. I've see similar errors before when the PK in the table was not set up as an IDENTITY column since the default for an int is 0 (so it's always non-null). – tvanfosson Mar 23 '11 at 02:27
  • thanks, I will try to check keys in my tables and then tell about results. – mimic Mar 23 '11 at 06:29
  • I've solved the problem. It turned out I had to set up nested reference entities only defining id, not the objects itself. And I forgot to update (apply changes) the new added entity. – mimic Mar 23 '11 at 23:54