title: Entity Framework Core using CurrentValues.SetValues to update entity properties from other entity failing due to primary key on entity
body: <p>Please see my original SO question here - <a href="https://stackoverflow.com/questions/62527460/entity-framework-core-efficient-way-to-update-entity-that-has-children-based-o">Entity Framework Core - efficient way to update Entity that has children based on JSON representation of entity being passed in via Web API</a></p>
  <p>This question details that I'm building an API using .NET Core, Entity Framework Core and PostgreSQL. I am trying to bring in an object via the API, convert it to a type of <code>Customer</code> entity, then use it to update an existing <code>Customer</code> entity so basically take all the modified properties that have changed between the original and update, and generate an update statement.</p>
  <p>This has failed, due to the below error:</p>
  <blockquote>
  <p>System.InvalidOperationException: The property 'CustomerInternalId' on entity type 'Customer' is part of a key and so cannot be modified or marked as modified. To change the principal of an existing entity with an identifying foreign key first delete the dependent and invoke 'SaveChanges' then associate the dependent with the new principal.</p>
  </blockquote>
  <p>I am unclear however, how I can perform the update while asking .NET Core / EF Core to just keep the primary key <code>CustomerInternalId</code> of the existing record?</p>
  <p>Obviously I don't want to change the primary key, that's probably the one thing I don't ever want to update. It's all the other properties that may be changed by the incoming data.</p>
  <p>Here's my sample method (it's half way through testing so there's bits of logging etc in there as I'm trying to figure this out).</p>
  <pre><code>    /// &lt;summary&gt;
      /// Update customer record - still working on this
      /// &lt;/summary&gt;
      /// &lt;param name=&quot;customerPayload&quot;&gt;&lt;/param&gt;
      public static void UpdateCustomerRecord(CustomerPayload customerPayload)
      {
          try
          {
              var updateCustomer = customerPayload.Convert(customerPayload);
              Console.WriteLine($&quot;Update customer created from payload&quot;);
  
              using (var loyalty = new loyaltyContext())
              {
                  Console.WriteLine($&quot;Using context to get db customer by mca id {updateCustomer.McaId}&quot;);
                  var customer = loyalty.Customer
                      .Include(c =&gt; c.ContactInformation)
                      .Include(c =&gt; c.Address)
                      .Include(c =&gt; c.MarketingPreferences)
                      .Include(c =&gt; c.ContentTypePreferences)
                      .Include(c =&gt; c.ExternalCards)
                      .Where(c =&gt; c.McaId == updateCustomer.McaId).First();
  
                  var cu = (from c in loyalty.Customer
                      where c.McaId == updateCustomer.McaId
                      select c).ToList();
  
                  Console.WriteLine($&quot;Customer guid from linq query {cu.First().CustomerInternalId}&quot;);
  
                  Console.WriteLine(
                      $&quot;db customer Id {customer.CustomerInternalId} incoming customer Id {updateCustomer.CustomerInternalId}&quot;);
  
                  loyalty.Entry(customer).CurrentValues.SetValues(updateCustomer);
                  loyalty.Entry(customer).State = EntityState.Modified;
                  Console.WriteLine($&quot;customer last name: {customer.LastName}&quot;);
                  loyalty.SaveChanges();
                  //TODO expand code to cover scenarios such as an additional address on an udpate
              }
          }
          catch (ArgumentNullException e)
          {
              Console.WriteLine(e);
              throw new CustomerNotFoundException();
          }
          catch (Exception ex)
          {
              Console.WriteLine($&quot;{ex}&quot;);
          }
      }
  }
  </code></pre>