title: Violation of PRIMARY KEY when trying to update entity
body: <p>I am trying to update an Entity in my database, but when I try to do so I get an error saying <code>Violation of PRIMARY KEY constraint 'PK_Units'.  Cannot insert duplicate key in object 'dbo.Units'.</code> </p>
  
  <p>I am not trying to insert new data with a key that already exists.  I am merely trying to retrieve an already existing item from the database, modify it, and then update it.</p>
  
  <p>Here, is the code that does this:</p>
  
  <pre><code>// Get an already existing unit from the repository
  Unit existingUnit = pacificRepo.GetUnit(unit.c_number);
  
  // Update a few values of this existing unit
  existingUnit.serial_number = unit.serial_number;
  existingUnit.country = unit.country;
  existingUnit.geo_location = unit.geo_location;
  
  // Commit the changes to the repository
  pacificRepo.UpdateUnit(existingUnit);
  </code></pre>
  
  <p>The supporting GetUnit() function used above is very simple:</p>
  
  <pre><code>public Unit GetUnit(string c_number)
  {
      return context.Units.FirstOrDefault(u =&gt; u.c_number == c_number);
  }
  </code></pre>
  
  <p>Here is the offending function:</p>
  
  <pre><code>public bool UpdateUnit(Unit u)
  {
      try
      {
          context.Entry(u).State = System.Data.Entity.EntityState.Modified;
          context.SaveChanges();
          return true;
      }
      catch (Exception e)
      {
          return false;  // Violation of PRIMARY KEY ....
      }
  
  }
  </code></pre>
  
  <hr>
  
  <p><strong>Edit</strong>:  Here is the Unit class</p>
  
  <pre><code>public class Unit
  {
      [Key]
      public string c_number { get; set; }
      public string serial_number { get; set; }
      public string ip_address { get; set; }
      public string build_version { get; set; }
      public string country { get; set; }
      public DbGeography geo_location { get; set; }
      public string location_at_address { get; set; }
      public int deployment_status { get; set; }
      public string short_description { get; set; }
      public string notes { get; set; }
      public int contact_id { get; set; }
      public int reg_contact_id { get; set; }
      public int network_id { get; set; }
  
      ...
  }
  </code></pre>
  
  <p>And the database:</p>
  
  <pre><code>CREATE TABLE [dbo].[Units] (
  [c_number]            NVARCHAR (45)     NOT NULL,
  [serial_number]       NVARCHAR (45)     NULL,
  [ip_address]          NVARCHAR (45)     NOT NULL,
  [build_version]       NVARCHAR (45)     NOT NULL,
  [geo_location]        [sys].[geography] NULL,
  [location_at_address] NVARCHAR (45)     NULL,
  [deployment_status]   INT               NOT NULL,
  [short_description]   NVARCHAR (255)    NULL,
  [notes]               TEXT              NULL,
  [contact_id]          INT               NULL,
  [reg_contact_id]      INT               NULL,
  [network_id]          INT               NULL,
  [country]             NVARCHAR (45)     NULL,
  CONSTRAINT [PK_Units] PRIMARY KEY CLUSTERED ([c_number] ASC)
  );
  </code></pre>
  
  <p>Does anyone know why this might be happening?  I am not trying to Add() it, I am trying to set its state to modified.</p>