none
Error on SaveChanges RRS feed

  • Question

  • Hi All

    I have an application that is using EF to maintain a main table with many other dependent tables linked to it. They all have ID fields that are the primary key and set as Identities and are Entity keys in EF. The dependent tables link back to the main table thought it’s ID field. They are data bound to grids on the same page.

    For each record added in the main table one or more records may be added in each of the dependent tables. EF and the bonded grids handle this without any problem. However when I try to apply SaveChanges (First with no parameters) I get an error that says  “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.“

    If I apply SaveChanges using the  SaveOptions.None” parameter it saves correctly without any errors. However because the “Acceptallchanges” does not get invoked, the state of the items stays as “Added” and is not changed to “Unchanged”. This result of this is that if the user clicks save again, all the records are added again and I end up with duplicate entries.

    I first thought that EF was not handling the ID keys properly but the bounded grids handle that correctly. i.e. if the user selects a row on the main grid, all the dependent grids change to display the correct records that link to that record in the main grid. So the IDs must be working.

    The only thing I could think of is that one of the tables’ ID is set to Unique but not Identity

     So it tries to write a 0 in the ID field more than once, generating the error, However I have been though all the tables both in SQL Server and in the entity and all the setting are correct.

    I am at a loss.

    If anyone has any ideas it would much appreciated

    Thanks in advance  

    Kent

    Thursday, July 12, 2012 3:31 PM

Answers

  • On 7/12/2012 1:00 PM, KentJ05 wrote:
    > Thanks
    >
    > That's what I have been doing in the mean time
    >
    > but that is not an acceptable solution.
    >
     
    EF is not a stops all and ends all solution, and it's not a savior, just
    like if you did it with straight-up T-SQL couldn't stop it.
     
    Thursday, July 12, 2012 5:23 PM

All replies

  • On 7/12/2012 11:31 AM, KentJ05 wrote:
     
    >
    > If I apply SaveChanges using the“SaveOptions.None” parameterit saves
    > correctly without any errors. However because the “Acceptallchanges”
    > does not get invoked, the state of the items stays as “Added” and is not
    > changed to “Unchanged”. This result of this is that if the user clicks
    > save again, all the records are added again and I end up with duplicate
    > entries.
     
    Then you must stop the user from clinking the button again by coming out
    of this whole add functionality on the screen.
     
    This is not an EF issue. It's a programming design issue where you have
    to stop the user or take the user out of the add functionality and make
    them do something to return to the add functionally and not let allow
    the user to sit there and continue to push the button. :)
     
    Thursday, July 12, 2012 4:09 PM
  • Thanks

    That's what I have been doing in the mean time

    but that is not an acceptable solution.

    Thursday, July 12, 2012 5:00 PM
  • On 7/12/2012 1:00 PM, KentJ05 wrote:
    > Thanks
    >
    > That's what I have been doing in the mean time
    >
    > but that is not an acceptable solution.
    >
     
    EF is not a stops all and ends all solution, and it's not a savior, just
    like if you did it with straight-up T-SQL couldn't stop it.
     
    Thursday, July 12, 2012 5:23 PM