Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0 C# ASP.NET C#4.0 MVC , +
table: Country
{
Country varchar(50)
}
 

sample Data:
Country
1. India
2. USA
 
What I want is Update Country field. But its primary key. So i got following error.
The property 'Country' is part of the object's key information and cannot be modified.

So please help me how to achieve this task.
 
Thanks in advance...
Posted 8-Mar-11 23:53pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi there...
 
You can update any field in a table. The reason, why you cannot edit the primary key value here, is that it (the primary key) has been referenced by another table as a foreign key. You will have to drop/change any referencing rows in other tables in order to update this "Country" column.
 
On a side note, in real world the best practice is to create a table called "Country" with 2 columns. One integer column which is the primary key called "ID" and the other column a varchar(50) named "description". You can make the ID column an auto-incremental field to reduce the hassle and make the "Description" column a unique index. Update the other tables to refer to the "ID" column as a foreign key. The key advantage in this approach is, you can change the description which is the name of the country to anything else without updating the ID, which I think what you want to achieve Smile | :)
 
Hope this helps
 
Regards, Pasan.
  Permalink  
v2
Comments
Dave Parth at 9-Mar-11 6:35am
   
Hi Pasan,
But its requirement not to create additional column of Id.
Other reason is that database huge containing more than hundreads of tables, so this change take much time and its not affordable.
 
So in this context, if you will provide me any solution then it will be really helpful to me.
 
By the way Thanks for reply.

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

  Print Answers RSS
0 _Amy 310
1 OriginalGriff 294
2 Nirav Prabtani 268
3 Sergey Alexandrovich Kryukov 211
4 CHill60 150
0 OriginalGriff 7,889
1 Sergey Alexandrovich Kryukov 7,006
2 Maciej Los 4,014
3 Peter Leow 3,708
4 CHill60 2,762


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 10 Nov 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100