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 9-Mar-11 0:53am
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 OriginalGriff 515
1 Maciej Los 349
2 Richard MacCutchan 220
3 BillWoodruff 209
4 Mathew Soji 160
0 OriginalGriff 8,654
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,229
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.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