locked
Delete records with Stored Procedure ... Refresh Context RRS feed

  • Question

  • Hi all. I am running a delete stored procedure which includes truncates and turning off triggers etc. 

    Here is the code...

     

    dbx.DELETE_RT_LOCATION(locationCode)

    dbx.SaveChanges()

    Problem is the dataContext seems to be out of synch. When I try and add a location that I just deleted I get a primary key error, i.e. its still there.

    Any help would be gratefully accepted

    Scott

     

     

    Friday, September 30, 2011 3:52 PM

Answers

  • Hi Scott,

    Welcome!

    You can try to use differernt instants of ObjectContext to delete(sp) and add new record. it is hard for EF to track the records which has been deleted by Stored Procedure(or directly delete from database). The workaround is open a new instance of ObjectContext.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Holger Kreissl Monday, October 3, 2011 12:38 PM
    • Marked as answer by scottsanpedro Monday, October 3, 2011 3:51 PM
    Monday, October 3, 2011 3:40 AM

All replies

  • Hi,

    The exact error message could help. It could help to see if EF complains that he already has an entity with this key or if SQL Server complains it really have another row with the same primary key.


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    Friday, September 30, 2011 4:18 PM
  • Thanks Patrice...I believe its EF

    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.

    Thanks Scott

    Friday, September 30, 2011 4:34 PM
  • Hi Scott,

    Welcome!

    You can try to use differernt instants of ObjectContext to delete(sp) and add new record. it is hard for EF to track the records which has been deleted by Stored Procedure(or directly delete from database). The workaround is open a new instance of ObjectContext.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Holger Kreissl Monday, October 3, 2011 12:38 PM
    • Marked as answer by scottsanpedro Monday, October 3, 2011 3:51 PM
    Monday, October 3, 2011 3:40 AM
  • Many thanks for your answer.
    I thought as much. I generally used the same ObjectContext across the application as a shared object.
    I will look into splitting it off 
    Cheers!
    Scott
    Monday, October 3, 2011 8:09 AM