title: Updating primary key in Entity Framework Code First
body: <p>I want to update a mediate table's primary keys. <a href="https://stackoverflow.com/questions/1367751/update-primary-key-value-using-entity-framework">This post</a> suggests to take another primary key and don't change it, but we're using our tables in other non-EF projects, I didn't designed them and I'm not able to change them. Do I have any other option? Anything? Even deleting the old record and inserting a new one. I just don't know how to retrieve the old values.</p>
  
  <p>This is my class:</p>
  
  <pre><code>public class ZChangeUnits : User
  {
      [Key]
      [Column(TypeName = "VARCHAR", Order = 0), StringLength(4)]
      public string CCode1 { get; set; }
  
      [ForeignKey("CCode1")]
      public virtual ZUnits ZUnits1 { get; set; }
  
      [Key]
      [Column(TypeName = "VARCHAR", Order = 1), StringLength(4)]
      public string CCode2 { get; set; }
  
      [ForeignKey("CCode2")]
      public virtual ZUnits ZUnits2 { get; set; }
  
      [Column(TypeName = "NUMERIC")]
      [DecimalPrecision(18, 5)]
      public decimal NZarib { get; set; }
  }
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I've posted the schema below, the left table is used for unit conversion. This is actually the suggested method in <a href="https://stackoverflow.com/a/7053393/3967440">this answer</a>.</p>
  
  <p>I can't think of any other way to implement that. Whether I should update my table's design or some EF code will do the job. Any help and solution is welcome. :)</p>
  
  <p><img src="https://i.stack.imgur.com/Qs5c8.png" alt="Table&#39;s relationships"></p>