LINQ. ATTACH returns error key in use RRS feed

  • Question

  • Hello:

    I'm trying to update my database using Linq, but I'm having problems:

    For my project, its needed to convert the table object to datatable (using reflection) and so, I change datatable column values. (ej:Datatable1["code"]="123") .

    After this, I again convert it to object, and try to update database with its new values in the existing datacontext. The problem is that both objects (converted from datatable and object in datacontext) have the same UID. So, if I try to use the "attach" method:


    WKCTA001)ObjectWXCTA,true); //AsModified

    It returns an error "Cannot add an entity with a key that is already in use"

    So, how can I to replace my object with the datacontext existing object?

    Thank you and regards.

    Thursday, October 15, 2009 5:01 PM


  • Hi pepep,


    Are you working with N-tier project?

    If it is that case, I recommend that you using a new datacontext for updating. To keep datacontext short lifetime is better in N-tier project.


    If it is not N-tier project, then you can choose not to use attach for updating. The normal updating way would be enough.


    If you have any questions or concerns, please update the thread and we will have a further discussion.



    Best Regards

    Yichun Feng

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Elkiwi Monday, October 19, 2009 11:15 AM
    Monday, October 19, 2009 9:01 AM

All replies