Click here to Skip to main content
15,904,156 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I try to Update its show this error.
"A member defining the identity of the object cannot be changed. Consider adding a new object with new identity and deleting the existing one instead."

Any solution for this. How to update PK & FK column.

Thanks
Posted

Why are you trying to update the primary key? Having a column that acts as both primary key and foreign key is a strange choice? I would suggest that you need to separate these fields out so that you have a separate FK and PK, then when you update your record you can update the foreign key without impacting the PK.

Saying that, you may have a reason that I can't see (and it would be the height of arrogance of me to assume I knew why you were trying to do something), so if you could explain further why they are the same field, we may be able to suggest alternatives.

[Edit]

I'm sorry to say, but your design has a fundamental problem - you really shouldn't use the same field for both. The best option would be to consider splitting the FK apart from the PK.

If you can't do that for some reason, you are going to have to use a stored procedure to perform the update. In your stored procedure, you need to disable the constraints using
SQL
ALTER TABLE tablename WITH NOCHECK CONSTRAINT ALL
Update your PK and then reenable your constraints.
 
Share this answer
 
v3
Comments
divesh12 19-Jul-11 12:53pm    
I have a table with 14 column but i use only 6 column for update. One of the column i use its PK and FK. But its compalsury for Update so i use it. Can any solution for that update that column?
Pete O'Hanlon 19-Jul-11 12:55pm    
Is the PK the same column as the FK?
divesh12 19-Jul-11 12:59pm    
yes
Pete O'Hanlon 19-Jul-11 13:21pm    
I have amended my reply, but I MUST caution you that your design really needs to be rethought.
divesh12 19-Jul-11 13:33pm    
I want this Update query in LINQ.
Ideally, a primary or foreign key should be chosen so that it never need be updated.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900