How can I resolve error: 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


  • I have a Services table with a One to Many relationship with a Projects table (actually, there are several other relationships, but my problem is impacting my attempt to Edit an existing Project related to an existing Service).  

    I am using a FormView and an EntityDataSource for controlling ReadOnly, Edit and Insert modes with templates for each mode.  I am using code behind logic to validate certain data entry fields and I started maintaining a shared Entity ObjectContext to help when navigating various related tables.

    In the ContextCreating event for the EntityDataSource, I started setting e.Context equal to my shared ObjectContext.

    The Updating event for the EntityDataSource fires and I do some validation, but before the ItemUpdating event of the associated FormView fires, I get the AcceptChanges error noted in the subject of this post.  Then, this ObjectContext shows 2 copies of the Project record I was trying to Update.

    To see if I could workaround this error, I updated the ContextCreating event to use a new instance of the ObjectContext, and I don't get the error and everything now seems to be working.

    Does anyone have any idea why using the shared ObjectContext causes the error, but creating a new instance does not?

    Thanks, Dean

    • تم النقل بواسطة Bob Wu-MT 16/شعبان/1433 09:30 ص (From:Windows Forms Data Controls and Databinding)
    23/رجب/1433 10:54 م

جميع الردود