none
How to update composite primary key fields? RRS feed

  • Question

  • Hi all,

     

    I quite new to EF4, T4 template and POCO, facing a problem now when try to update a table with composite primary keys.

    I have a table with this structure:

    TableA(ID1, ID2), ID1 and ID2 are composite primary key.

     

    When I try to change the value of ID2, the EF4 will prompt me this message:

    The property 'ID2' is part of the object's key and cannot be changed. Changes to key properties can only be made when the object is not being tracked or is in the Added state.

     

    I tried to set the change tracker = false but the row status will remain Unchanged even I successfully made the change to ID2 field, therefore EF4 cannot detect changes and no update is trigger.

     

    I know there are some arguments for the composite primary key structure, however in my case I not able to change the structure of the table, what I can do is try my best to find workaround from code/EF4.

     

    Anyone please advise? Thanks.

     


    Tuesday, August 9, 2011 2:45 AM

Answers

  • I using POCO + EF4, and I hit the error message above when I try to change the value in one of the primary key field, as what i mentioned in first thread I got a simple table with 2 fields, and both fields are primary key field.

     

    I think this is the restriction of EF4 since i trying to change the value in PK field, however I figures out the alternate way, which is delete the row and add a new row into table, this solve my problem.

     

    Thanks.

     

    • Marked as answer by tzuhsun Thursday, August 18, 2011 1:41 AM
    Thursday, August 18, 2011 1:40 AM

All replies

  • Hello,

    Could you please provide more detailed code snippets or any demo code fragment in your reply? I think more detailed information can help us analyze your question better.

    Please feel free to let us know if you have any finding.

    Best Regards,


    Larcolais Gong[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.

    Thursday, August 11, 2011 8:06 AM
  • I using POCO + EF4, and I hit the error message above when I try to change the value in one of the primary key field, as what i mentioned in first thread I got a simple table with 2 fields, and both fields are primary key field.

     

    I think this is the restriction of EF4 since i trying to change the value in PK field, however I figures out the alternate way, which is delete the row and add a new row into table, this solve my problem.

     

    Thanks.

     

    • Marked as answer by tzuhsun Thursday, August 18, 2011 1:41 AM
    Thursday, August 18, 2011 1:40 AM