Error inserting new items to the database

May 8, 2013 at 7:19 AM
Edited May 8, 2013 at 4:00 PM
Hi everybody,

I have a Windows forms application, using my nHydrate model. I have dragged-n-dropped an entity from my datasources window on a form as "Details" view, which created the various textboxes etc and also a BindingNavigator.

In the BindingNavigator's "Save" button I added a context.SaveChanges() line and all updates work fine. To implement the insert behaviour, I added the following code in the "Add" button of the BindingNavigator:
            var p = MyBindingSource.Current as MyEntity;
The Add button click clears the Textboxes. I fill the textboxes with the new values and press the Save button. This gives me the following exception:

The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception 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.

Any idea??

Please note that the changes are indeed saved in the database. MyEntity has an integer primary key with its identity property set to "Database" (i.e. autoincrement).

Best regards,

Following this suggestion

I changed the context.SaveChanges() to
and it stopped giving the exception.
I'd be very grateful if an expert could tell me what are the implications of this for nHydrate.