How to update primary key value in entity framework?

10,803

Solution 1

As mentioned by the others, you can't do it in code. You will have to make your update in SQL. Either in a migration or directly in SQL Server Management Studio (or the equivalent if you're using a different database).

UPDATE Person -- Or 'Persons' if that's what your table is called
SET SocialID = SocialID + '00'

It will require a lot more work than this if you have other tables use this column as a foreign key (you'll have to drop the constraints first -- on all tables that reference your primary key -- then fix the data and recreate the constraints). Or as Moe said in the comments, you can set your foreign keys to cascade on update.

Solution 2

As per my knowledge primary key once generated cant be updated programatically,that defies the purpose of primary key. It'll be better if you insert all your data again with new primary keys and delete old data.

Share:
10,803

Related videos on Youtube

Seyed Morteza Mousavi
Author by

Seyed Morteza Mousavi

Hi, I am a software developer and sometimes write blog posts: https://mousavi310.github.io/ https://medium.com/@mousavi310

Updated on September 16, 2022

Comments

  • Seyed Morteza Mousavi
    Seyed Morteza Mousavi over 1 year

    I have table(and also entity in Entity Data Model) Person with following fields:

    Name              Type
    SocialID          String      
    FirstName         String
    LastName          String
    

    which SocialID is Primary Key. I want to update value of SocialID for each record. However when i try to update this field in Entity Framework I get following error:

    The property 'SocialID' is part of the object's key information and cannot 
    be modified.
    

    The code that i get above error is:

    foreach (var p in Entity.Persons)
    {
       p.SocialID= p.SocialID + "00";
       Entity.SaveChanges();
    }
    

    How I can do this??

  • Seyed Morteza Mousavi
    Seyed Morteza Mousavi about 11 years
    Wow! I need previews data!
  • Seyed Morteza Mousavi
    Seyed Morteza Mousavi about 11 years
    Because of changing Social style
  • Miguel Matos
    Miguel Matos about 11 years
    @SeyedMortezaMousavi In that case you are not talking about a primary key but a foreign key. You should add a new key to that table identifing the person. SocialId should be the primary key of the Social style table.
  • Kirill Bestemyanov
    Kirill Bestemyanov about 11 years
    If you want to update primary key for all record in table - use database script. Entity Framework don't permit changing of primary key.
  • Miguel Matos
    Miguel Matos about 11 years
    @KirillBestemyanov based on his comment I don't think he wants to do that.
  • nerdybeardo
    nerdybeardo about 11 years
    You can change your foreign key to cascade on update, that should make it easier. The other option is to drop the foreign key, update table A then table B and then re-create the foreign key.
  • Kirill Bestemyanov
    Kirill Bestemyanov about 11 years
    I am afraid that SocialId is natural key (and there is no foreign key) and format of this key was changed.
  • Bennor McCarthy
    Bennor McCarthy about 11 years
    Why do you have to do it programmatically. Primary keys are not really supposed to change at all (even though there's sometimes a need to make a one-off change to them). Doing it in code suggests you're doing it more than once, which is probably wrong and maybe the column you're updating shouldn't be the key.