title: EF Update all the entity properties
body: <p>I'm using a generic service for CRUD operations. When I try to update an entity, I get the following error :</p>
  <blockquote>
  <p>The property 'TestClass.Id' 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', and then associate the dependent with the new principal.</p>
  </blockquote>
  <p>Here is the code of the service :</p>
  <pre><code>public virtual async Task&lt;TDto&gt; Update(IdType id, TRequest request)    
  {
      var repo = dbModel.Set&lt;T&gt;();
      var entity = await repo.Single(e =&gt; e.Id.Equals(id));
      entity = _mapper.Map(request, entity);
      repo.Update(entity);
      await dbModel.SavesChanges();
      var ret = _mapper.Map&lt;TDto&gt;(entity);
      return ret;
  }
  </code></pre>
  <p>The entity comes from the repo, so all of its properties are fulfilled from the database, and it should be tracked by EF. Then I map the updated properties from the request with AutoMapper. For example I am using these classes to test my code :</p>
  <pre><code>public class TestClass
  {
      public int Id { get; set; }
      public DateTime CreationDate { get; set; }
      public string Comment { get; set; }
  }
  
  public class TestClassRequest
  {
      public string Comment { get; set; }
  }
  </code></pre>
  <p>The &quot;entity&quot; var is fulfilled with all the properties of the <code>TestClass</code>, then with the mapper only the <code>Comment</code> property is updated. Everything works great.</p>
  <p>But when I try to save the changes to the database I have the error I stated before.</p>
  <p>I assume that the <code>repo.Update()</code> tries to update all the properties, even the id, as stated in this explication from <a href="https://www.learnentityframeworkcore.com/dbset/modifying-data" rel="nofollow noreferrer">learnentityframeworkcore.com</a> :</p>
  <blockquote>
  <p>This method results in the entity being tracked by the context as Modified. The context doesn't have any way of identifying which property values have been changed, and will generate SQL to update all properties</p>
  </blockquote>
  <p>So I added the line <code>dbModel.ChangeTracker.DetectChanges();</code> before calling the <code>SavesChanges()</code> but I still get the same error.</p>
  <p>Is there a way to update only modified properties with the <code>dbModel.DbSet&lt;T&gt;</code> ?</p>